0

In Haskell or ML it's perfectly fine to omit types of the arguments in a function:

fun add (x, y) = x + y (*ML*)
add x y = x + y -- Haskell

because it's clear from a context what the types of the parameters should be. Why isn't that the same in Scala?

def add(x, y) = x + y // error!
Incerteza
  • 32,326
  • 47
  • 154
  • 261
  • *Very* briefly - the OO nature of Scala complicates type inference, so Scala is (thus far) not able to perform as well as ML and Haskell in this regard - see the link given by @Tikhon for more. – Shadowlands Oct 29 '13 at 06:53
  • Related: [Why is Scalas type inference not as powerful as Haskells?](http://stackoverflow.com/questions/7234095/why-is-scalas-type-inference-not-as-powerful-as-haskells) – kiritsuku Oct 29 '13 at 07:28
  • Try: `def add[B](x:B,y:B)(implicit num: Numeric[B]):B = num.plus(x,y)` – Andrzej Jozwik Oct 29 '13 at 07:29
  • @ajozwik, this is not type inference. – Incerteza Oct 29 '13 at 11:37
  • @Alex - but in scala you cannot write without knowledge about types. See implementation of List, Seq for sum, Ordered and other. Maybe you will find something more friendly – Andrzej Jozwik Oct 29 '13 at 15:23
  • @ajozwik, why did you give me that example then? – Incerteza Oct 29 '13 at 15:50

0 Answers0