This page is no longer maintained — Please continue to the home page at

Problem passing a PartialFunction

No replies
Carsten Saager
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.

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)} //
<console>:7: error: type mismatch;
 found   : PartialFunction[String,Unit]
 required: PartialFunction[Any,Any]
Which is is correct as PartialFunction is contravariant in the first type

Next try
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


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland