I've been wondering, why this piece of code won't compile?
Is there a way in Scala to create method/func that is generic parametrised and allows for such operation like 'reduce'.
Is this behaviour having anything in common with type erasure or is it something else? I would love to see broad explanation of this :)
def func2[B <: Int](data: Seq[B]): Unit = {
val operation = (a: B, b: B) => a.-(b)
data.reduce(operation)
}
Compiler says:
type mismatch;
found : (B, B) => Int
required: (Int, Int) => Int
Also, in same spirit - is it possible overall to call any 'stream-like' method, on parametrized collection with this method:
def func2[B <: Int](data: Seq[B]): Unit = {
val operation = (a: B, b: B) => a.-(b)
data.sum
}
also gives:
could not find implicit value for parameter num: Numeric[B]