Why scala cannot infer the type of method parameters

I am wondering why scala cannot infer the type of the method parameters. I see that in haskel (which also has type inference) can do the same. Then why not for scala?

+4
source share
2 answers

First of all, the situation in Scala is quite different from the situation in Haskell, because the OO language and type-output in an object-oriented setup are a little more complicated.

The only OO language I know that is suitable for full type inference is OCaml. OCaml does this by extensively using structural typing (the assumed type o in let fo = o.foo 42 is β€œAn object that has a method foo that takes an int as an argument,” and the return type is β€œall return type o.foo is ", which is the only useful type for output here).

However, Scala has many additional features (overloading, implicit conversions) that interfere with the OCaml approach and make it impossible to fully infer a global type.

+11
source

Simply put, Hindley-Milner, the type inference algorithm used by Haskell, does not work in the presence of subtyping.

+6
source

All Articles