I am doing a simple exercise that asks me to implement the standalone "map" function in a list using foldRight. And the solution I came up with is the following:
def mapFun[T,U](xs: List[T], f: T => U): List[U] = {
(xs foldRight List[U]())((x, y) => f(x) :: y)
}
val sl = List(1,2,3)
//now try to square every item
mapFun(sl, x => x * x) //**missing parameter type**
mapFun(sl, (x:Int) => x * x) //ok, gives List(1,4,9)
As indicated above, the explicit type "Int" must be specified for the compiled code. However, it seems to me that the compiler should be able to infer the type "x", since "sl" is of the type "List [Int]", which means that T is "Int", and then the expression "x * x" of type U should also be "int".
I suggest that this may have something to do with the variety or variety of the controversy, or something where subtyping mixes with typical typing.
My scala version of the compiler is a package 2.11 (dynamics).
: Scala 3:
scala; , Haskell OCaml, , .