So far i was under the impression that the only way to define Generic function in scala, was using Method e.g.
def myToString[A](value: A) = {println(value)}
However i figured the following way:
val myToStringFun: (T forSome {type T}) => Unit = value => println(value)
Is there something that i am not seeing or is that the pattern to write generic function in Scala without resorting to method ?
I never seen that pattern before, and only came up with it, based my learning around Higher-Kinded Type and the notion of existential ...
Please share your thoughts or wisdom on this....
EDIT1:
If the above is correct, why is it that this pattern is not used and people systematically resort to Method for generic function. Is it just a ease of notation
EDIT2:
If a function (T forSome {type T}) => Unit <=> Any => Unit
It seems however that
val mySeqToString: (Seq[T] forSome {type T}) => String = {
case head +: tail => s"$head +: " + seqToString(tail)
case Nil => "Nil"
}
is equivalent to
def seqToString[T](seq: Seq[T]): String = seq match {
case head +: tail => s"$head +: " + seqToString(tail)
case Nil => "Nil"
}
correct ?