Say we have a Scala trait
trait Foo {
def bar(baz: Qux): Quux
def corge: Grault
}
and we want to implement it in such a way that the implementation of bar() can be handled by an anonymous function. We can do this:
class FooImpl1(_bar: (Qux) => Quux, val corge: Grault) extends Foo {
override def bar(baz: Qux) = { _bar(baz) }
}
allowing, e.g.:
val foo: Foo = new FooImpl({ qux: Qux => qux.asQuux()}, someGrault)
But this seems unnecessarily verbose (and also introduces the dreaded, and clunky, leading underscore).
Naively, noting that def corge in Foo is implemented by val corge in FooImpl1, it seems it should be possible to do something like this:
class FooImpl2(val bar: (Qux) => Quux, val corge: Grault) extends Foo {}
But we can't, presumably because Foo.bar doesn't return a function, it is a function (or rather a method).
Is there anything cleaner than FooImpl1 that we can do here?