Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why scala can't infer the type of method parameters

Tags:

scala

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

like image 965
Byju Veedu Avatar asked Dec 02 '22 04:12

Byju Veedu


2 Answers

First of all the situation in Scala is quite a bit different than in Haskell because it's an OO language and type-inference in an object oriented setting is a bit more complicated.

The only OO language that I know which comes close to full type inference is OCaml. OCaml does so by making extensive use of structural typing (the inferred type of o in let f o = o.foo 42 is "Object which has a foo method which takes an int as an argument" and the inferred return type is "whatever the return type of o.foo is", which is the only useful type to infer here).

However Scala has many additional features (overloading, implicit conversions) that get in the way of OCaml's approach and make full, global type inference impossible.

like image 200
sepp2k Avatar answered Dec 18 '22 09:12

sepp2k


To put it simply, Hindley-Milner, the type inference algorithm used by Haskell, doesn't work in the presence of subtyping.

like image 45
Daniel C. Sobral Avatar answered Dec 18 '22 10:12

Daniel C. Sobral