4

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 ?

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Especially overloading is a problem for Hindley-Milner style type inference.
7

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

1 Comment

Except a simply modified version does work in OCaml, which has subtyping. Of course, it's structural subtyping rather than Scala's nominative subtyping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.