Type parameters applied to Scala Function

后端 未结 6 1079
遇见更好的自我
遇见更好的自我 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 09:00

    Try

    def myfunc(f:String => Int):Int =  {
      Integer.min(1,2)
    }
    

    When you write def myfunc[Int](f:String => Int):Int you declare type parameter Int, which hides standard type scala.Int. This is the same as if you declared def myfunc[A](f:String => A):A. When you remove return type it's inferred to be scala.Int, i.e. def myfunc[A](f:String => A) is def myfunc[A](f:String => A):Int

提交回复
热议问题