0

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?

2 Answers 2

3

val bar: String => Int = s => s match { case "a" => 123 case _ => s.length() }

or just

val bar = (s: String) => s match { case "a" => 123 case _ => s.length() }

or better yet:

val bar: String => Int = { case "a" => 123 case s => s.length() }

And, to take it in a different direction...

val bar = { case "a" => 123 case s => s.length() }: String => Int

Sign up to request clarification or add additional context in comments.

Comments

2

I believe the following is what you want:

val bar: String => Int = s => s match {case "a" => 123 case _ => s.length ()}

or more concisely:

val bar: String => Int = { case "a" => 123 case s => s.length ()}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.