- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
case objects and pattern matching
Mon, 2011-12-19, 19:43
Hi guys, I am walking my first steps in scala, I am trying to program
a bit with actors and I have the following problem with which you
might be able to help me.
this is the main loop of an actor.
def act() = {
loop {
react{
case s: Information => receive(s)
case s: ControlMessages => s match {
case Delay(time) =>
Thread.sleep(time)
}
}
send()
}
}
}
Delay is a subclass of ControlMessages. I get the following exception:
error: pattern type is incompatible with expected type;
found : object gainlo.Delay
required: gainlo.ControlMessages
case Delay => Thread.sleep(time)
thanks for any help.
gabriel
Mon, 2011-12-19, 20:31
#2
Re: case objects and pattern matching
yep, that would work, but I have many different group of messages and
I thought the code would be more readable this way. I would also like
to understand why these does not work. It seems that the type
inference has set s to be ControlMessages and that is why I get the
type mismatch error.
On Mon, Dec 19, 2011 at 4:09 PM, Kevin Wright wrote:
> Why not just:
>
> def act() = {
> loop {
> react {
> case s: Information => receive(s)
> case Delay(time) => Thread.sleep(time)
> }
> send()
> }
> }
>
> On Monday, 19 December 2011, gabriel wrote:
>>
>> Hi guys, I am walking my first steps in scala, I am trying to program
>> a bit with actors and I have the following problem with which you
>> might be able to help me.
>>
>> this is the main loop of an actor.
>>
>>
>>
>> def act() = {
>> loop {
>> react{
>> case s: Information => receive(s)
>> case s: ControlMessages => s match {
>> case Delay(time) =>
>> Thread.sleep(time)
>> }
>> }
>> send()
>> }
>> }
>> }
>>
>>
>> Delay is a subclass of ControlMessages. I get the following exception:
>>
>> error: pattern type is incompatible with expected type;
>> found : object gainlo.Delay
>> required: gainlo.ControlMessages
>> case Delay => Thread.sleep(time)
>>
>> thanks for any help.
>>
>> gabriel
>>
>>
>
Tue, 2011-12-20, 01:41
#3
Re: case objects and pattern matching
Perhaps, simply a tipo somewhere?
> case Delay(time) =>
> Thread.sleep(time)
> ...
> Delay is a subclass of ControlMessages. I get the following exception:
>
> error: pattern type is incompatible with expected type;
> found : object gainlo.Delay
> required: gainlo.ControlMessages
> case Delay => Thread.sleep(time)
^^^^^^^^^^^^
Why no parameter in Delay here?
def act() = {
loop {
react {
case s: Information => receive(s)
case Delay(time) => Thread.sleep(time)
}
send()
}
}
On Monday, 19 December 2011, gabriel wrote: