- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Problem passing a PartialFunction
Fri, 2008-12-26, 14:07
Hi,
I am a bit puzzled - perhaps I stumbled upon a bug (2.7.3RC1)
val ps:PartialFunction[String,Unit] = {case msg:String => println(msg)}
def none(f:PartialFunction[_,_]
none(ps) // fine
none {case msg:String => println(msg)}
<console>:6: error: missing parameter type for expanded function ((x0$1) => x0$1
match {
case (msg @ (_: String)) => println(msg)
})
none {case msg:String => println(msg)}
This is the same as why val pn = {case msg:String => println(msg)} doesn't work
So I tried
def any(f:PartialFunction[Any,Any]){}
any {case msg:String => println(msg)} //
any(ps)
<console>:7: error: type mismatch;
found : PartialFunction[String,Unit]
required: PartialFunction[Any,Any]
any(ps)
Which is is correct as PartialFunction is contravariant in the first type
Next try
nothing(f:PartialFunction[Nothing,Any]){}
nothing(ps) //fine
nothing {case msg:String => println(msg)}
<console>:6: error: pattern type is incompatible with expected type;
found : String
required: Nothing
nothing {case msg:String => println(msg)}
Either I miss something or the type of the adhoc PartialFunctions is off
/Carsten
I am a bit puzzled - perhaps I stumbled upon a bug (2.7.3RC1)
val ps:PartialFunction[String,Unit] = {case msg:String => println(msg)}
def none(f:PartialFunction[_,_]
none(ps) // fine
none {case msg:String => println(msg)}
<console>:6: error: missing parameter type for expanded function ((x0$1) => x0$1
match {
case (msg @ (_: String)) => println(msg)
})
none {case msg:String => println(msg)}
This is the same as why val pn = {case msg:String => println(msg)} doesn't work
So I tried
def any(f:PartialFunction[Any,Any]){}
any {case msg:String => println(msg)} //
any(ps)
<console>:7: error: type mismatch;
found : PartialFunction[String,Unit]
required: PartialFunction[Any,Any]
any(ps)
Which is is correct as PartialFunction is contravariant in the first type
Next try
nothing(f:PartialFunction[Nothing,Any]){}
nothing(ps) //fine
nothing {case msg:String => println(msg)}
<console>:6: error: pattern type is incompatible with expected type;
found : String
required: Nothing
nothing {case msg:String => println(msg)}
Either I miss something or the type of the adhoc PartialFunctions is off
/Carsten