I'm very new to scala so you may forgive me this basic newbie question. I have following code snippet.
type Pred[A, B] = (A, B) => Int;
def abs[A, B](p: (A, B) => Int): (A, B) => Int =
(a, b) => if (p(a, b) < 0) -(p(a, b)) else (p(a, b));
def multiply(a: Int, b: Int): Int =
a * b;
def subtract(a: Int, b: Int): Int =
a - b;
val absMultiply = abs(multiply);
val absSubstract = abs(subtract);
println("abs(10 * 10) = " + absMultiply.apply(10, 10));
println("abs(10 - 100) = " + absSubstract.apply(10, 100));
So as I've understand it correctly it should be possible to replace (A,B) => Int with Pred[A, B].
def abs[A, B](p: Pred[A, B]): Pred[A, B] =
(a, b) => if (p(a, b) < 0) -(p(a, b)) else (p(a, b));
But then I get an compiler exception
type mismatch; found : (Int, Int) => Int required: (A, B) => Int
type mismatch; found : (Int, Int) => Int required: (A, B) => Int
Have I misunderstood here something wrong?
Thank you very much for your help.
Cyrill