I have an abstract class
abstract class Foo {
def foo(a: Int): Int
...
}
new Foo {
def foo(a: Int) = {
println("Foo")
a
}
}
I often see a companion object to make it a little less verbose for callers (like the Play framework).
object Foo {
def apply(f: Int => Int) = new Foo {
def foo(a: Int) = f(a)
}
}
Foo { a =>
println("Foo")
a
}
But suppose I make a generic method
abstract class Foo {
def foo(a: T): T
...
}
new Foo {
def foo(a: T) = {
println("Foo")
a
}
}
Can I use a companion object, i.e. Can I apply general type parameters to a function, and not to a method or class?
source
share