Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement a method using a function value in Scala

Tags:

scala

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 }
like image 627
Michał Bendowski Avatar asked Dec 23 '22 06:12

Michał Bendowski


1 Answers

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 }
like image 186
deamon Avatar answered Jan 05 '23 14:01

deamon