I have an abstract class
abstract class Foo {
def foo(a: Int): Int
...
}
// Usage
new Foo {
def foo(a: Int) = {
println("Foo")
a
}
}
I frequently see a companion object to make this a little less verbose for callers (e.g. the Play framework).
object Foo {
def apply(f: Int => Int) = new Foo {
def foo(a: Int) = f(a)
}
}
// Usage
Foo { a =>
println("Foo")
a
}
But suppose I make the method generic
abstract class Foo {
def foo(a: T): T
...
}
// Usage
new Foo {
def foo(a: T) = {
println("Foo")
a
}
}
Can I still use a companion object, i.e. can I apply generic type parameters to a function, rather than a method or class?