Type parameters applied to Scala Function

后端 未结 6 1069
遇见更好的自我
遇见更好的自我 2021-01-23 08:35

I am trying to understand the type parameters when applied to a function.

I would like to use Generic Types in the below method but using String and Int for my understa

6条回答
  •  死守一世寂寞
    2021-01-23 08:55

    Question: What's the difference between these 3 methods?

    def myfunc1[X](f:String => X):X =
      Integer.min(1,2)
    
    def myfunc2[Int](f:String => Int):Int =
      Integer.min(1,2)
    
    def myfunc3[IDontGetTypeParameters](f:String => IDontGetTypeParameters):IDontGetTypeParameters =
      Integer.min(1,2)
    

    Answer: Nothing. From the compiler's point of view they are the same, and they fail to compile for the same reason: each is defined to return the type of the type parameter but tries to return an integer (Scala.Int) instead.

提交回复
热议问题