Is it possible to provide an implementation of a method using a function object/value? I would like to write something in the spirit of:
abstract class A { def f(x:Int) : Int }
class B extends A { f = identity }
Is it possible to provide an implementation of a method using a function object/value? I would like to write something in the spirit of:
abstract class A { def f(x:Int) : Int }
class B extends A { f = identity }
You can use a field of type function like this:
abstract class A { val f: (Int) => Int}
val identity = (x: Int) => x*x
class B extends A { override val f = identity }
And to complement deamon and Daniel, here's another:
abstract class A { def f: (Int)=>Int }
class B extends A { val f = identity _ }
class C extends A { def f = identity _ }
class D extends A { def f = (x:Int) => -x }
If you are stuck with a normal def, then the best you can do is
abstract class Z { def f(x:Int):Int }
class Y extends Z { def f(x:Int) = identity(x) }