I'm working with Scala 2.9.2. I'd like to assign a unnamed function to a variable, but I can't seem to get the syntax straight. If I define a named function, and then assign the named function to the variable, it seems to work OK, as shown by the session here.
$ scala
Welcome to Scala version 2.9.2 (OpenJDK Server VM, Java 1.7.0_65).
Type in expressions to have them evaluated.
Type :help for more information.
scala> type Foo = String => Int
defined type alias Foo
scala> def myFoo (s : String) : Int = s match {case "a" => 123 case _ => s.length ()}
myFoo: (s: String)Int
scala> val foo : Foo = myFoo
foo: String => Int = <function1>
scala> foo ("b")
res0: Int = 1
scala> foo ("a")
res1: Int = 123
So far, so good. At this point I am thinking that I can define an unnamed function and assign it to a variable, but it appears I can't figure out the syntax. I tried several variations and none of them worked.
scala> val bar : Foo = (s : String) : Int = s match {case "a" => 123 case _ => s.length ()}
<console>:1: error: ';' expected but '=' found.
val bar : Foo = (s : String) : Int = s match {case "a" => 123 case _ => s.length ()}
^
scala> val bar : Foo = (s : String) => Int = s match {case "a" => 123 case _ => s.length ()}
<console>:8: error: reassignment to val
val bar : Foo = (s : String) => Int = s match {case "a" => 123 case _ => s.length ()}
^
scala> val bar : Foo = ((s : String) => Int) = s match {case "a" => 123 case _ => s.length ()}
<console>:1: error: ';' expected but '=' found.
val bar : Foo = ((s : String) => Int) = s match {case "a" => 123 case _ => s.length ()}
^
scala> val bar : Foo = (s : String) : Int => s match {case "a" => 123 case _ => s.length ()}
<console>:1: error: ';' expected but 'match' found.
val bar : Foo = (s : String) : Int => s match {case "a" => 123 case _ => s.length ()}
^
scala>
Sorry for the elementary question, but can someone point out the correct syntax?