Can we call scala functions polymorphic in parameter without formless

I am talking about this (and only about parametric polymorphism):

def fun1[T](s: Set[T]): Option[T] = s.headOpton

I have seen in several places when people call this an example of a polymorphic function around Tin raw scala - something like formless Set ~> Option.

But I do not understand why we can call it polymorphic. Despite the fact that we seem to be able to go there as Set[Int]well as Set[String]in fact, fun1[Int]and fun1[String]- two different functions. I think I can argue that since the eta extension fun1is wrong and does not give us what we want:

scala> fun1 _
res0: Set[Nothing] => Option[Nothing] = <function1>

And we should always indicate the type when expanding:

scala> fun1[Int]_
res1: Set[Int] => Option[Int] = <function1>

, scala - , . ?

+4
1

fun1 T, :

, , .

( Wikipedia)

, , fun1[Int] fun1[String] "- ". " ":

, ( ), , .

( Haskell wiki)


@Travis @Miles:

, "plain scala", . Miles Poly , :

object headOption extends PolyFunction1[Set, Option] {
  def apply[T](l: Set[T]): Option[T] = l.headOption
}

scala> headOption(Set(1, 2, 3))
res2: Option[Int] = Some(1)
+2

Source: https://habr.com/ru/post/1619267/


All Articles