- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Another Pattern Matching Question
Sun, 2009-07-19, 23:18
hi,
why in the following example i cannot use pattern matching:
class GenerateContext( val server: Server ) extends Context {
var msgs : List[ OSCMessage ] = Nil
var asyncMsgs : List[ OSCMessage ] = Nil
def add( msg: OSCMessage ) : Unit = {
msg match {
case AsyncMsg => asyncMsgs ::= msg
case _ => msgs ::= msg
}
}
}
where
trait AsyncMsg {}
and for example
context.add( new OSCMessage( "/b_free", Array( bufNum.asInstanceOf
[ AnyRef ]) with AsyncMsg )
the compiler says
"not found: value AsyncMsg"
(although i have a corresponding import statement and there is not
mistake in the import line)
thanks, -sciss-
Sun, 2009-07-19, 23:47
#2
Re: Another Pattern Matching Question
On Mon, Jul 20, 2009 at 12:17:54AM +0200, Sciss wrote:
> def add( msg: OSCMessage ) : Unit = {
> msg match {
> case AsyncMsg => asyncMsgs ::= msg
> case _ => msgs ::= msg
> }
> }
>
> trait AsyncMsg {}
Because you're matching on the value AsyncMsg, not the type. The type
would look like
case x: AsyncMsg => asyncMsgs ::= x
You need to write:
msg match {
case _ : AsyncMsg => asyncMsgs ::= msg
case _ => msgs ::= msg
}
The underscore means "don't name the value". The ": " means must
be of type .
-Arthur
On Sun, Jul 19, 2009 at 3:17 PM, Sciss wrote:
> hi,
>
> why in the following example i cannot use pattern matching:
>
> class GenerateContext( val server: Server ) extends Context {
> var msgs : List[ OSCMessage ] = Nil
> var asyncMsgs : List[ OSCMessage ] = Nil
>
> def add( msg: OSCMessage ) : Unit = {
> msg match {
> case AsyncMsg => asyncMsgs ::= msg
> case _ => msgs ::= msg
> }
> }
> }
>
> where
>
> trait AsyncMsg {}
>
> and for example
>
> context.add( new OSCMessage( "/b_free", Array( bufNum.asInstanceOf[
> AnyRef ]) with AsyncMsg )
>
> the compiler says
>
> "not found: value AsyncMsg"
>
> (although i have a corresponding import statement and there is not mistake
> in the import line)
>
>
>
> thanks, -sciss-
>
>