Hi,
I have a question about value discarding (discussed in section 6.26.1 of the Scala Language Specification).
Given the following method and a Unit-returning function...
def executeWithFoo(func: (String => Unit)) = { func("foo") }
val func1 = { string: String =>
Console.println(string)
}
...the method works fine:
scala> executeWithFoo(func1)
foo
However, given a function returning something other than Unit...
val func2 = { string: String =>
Console.println(string)
0
}
...the method fails:
scala> executeWithFoo(func2)
<console>:13: error: type mismatch;
found : String => Int
required: String => Unit
executeWithFoo(func2)
^
Why is the function's non-Unit return type not discarded? If I want the method to accept either function, is my best bet to change the type of its argument to (String => Any)? As a way to say, "if the function returns a value, the method does nothing with it", I find (String => Any) less clear than (String => Unit)...but maybe that's just me?
Thanks,
Dan