trait MyFunctionTrait extends ((Int, Int) => Double)
class MyFunction1 extends MyFunctionTrait {
override def apply(a: Int, b: Int) => Double = a/b
}
object MyFunction2 extends MyFunctionTrait {
override def apply(a: Int, b: Int) => Double = a/b
}
I am not entirely sure which one to use how. Is the differency how to run them?
scala> val f = new MyFunction1
f: MyFunction1 = <function2>
scala> f(1,2)
res50: Double = 0.0
scala> MyFunction2(1,2)
res48: Double = 0.0
I do know what the difference between a singleton object and a class is. I want to know the use-cases in the particular case of defining a Function. A Function is explained in articles, but I have seen it programmed as a class and as an object, so I'd like to know what best practise is when and why.
In my particular case I want to give another Function as a parameter to the Function in a currying style and then have different MyFunction
which do slightly different stuff depending on what Function they got: How to write a currying Scala Function trait?
Please answer for 1) the general case, 2) the particular case.
PS: I tried to separate the question on how to curry and whether class or object - did not seem to work so well, right here.