Consider:
trait Validation { def isValid(str: String): Boolean } class AlwaysValid extends Validation { override def isValid(_: String) = true }
gives
<console>:1: error: identifier expected but '_' found. override def isValid(_: String) = true
Any ideas why? Or is it just what language designers missed?
Maybe this concerns passing the argument name, but this only applies to non-overrides, since in any case it overrides auto- "inherit" the argument names from the overridden method, so this may not be the case:
trait Foo { def bar(arg0: String): String } class Baz extends Foo { override def bar(blabla: String) = "hello" } new Baz().bar(arg0 = "world")
Also: _ allowed in lambdas, even several times:
scala> val x: Int => Int = _ => 3 x: Int => Int = <function1> scala> val x: (Int, Int) => Int = (_, _) => 3 x: (Int, Int) => Int = <function2>
scala
Erik allik
source share