This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why are type annotations necessary for leaving arguments out?

3 replies
hseeberger
Joined: 2008-12-27,
User offline. Last seen 1 year 25 weeks ago.
Hi,
Why is it necessary to use a type annotation when leaving an argument out? The compiler knows the type as you can take from here:
scala> val add = (x: Int, y: Int) => x + y add: (Int, Int) => Int = <function2>
scala> val addOne = add(1, _: String)<console>:5: error: type mismatch; found   : String required: Int        val addOne = add(1, _: String)
Why is the following not possible?
scala> val addOne = add(1, _)<console>:5: error: missing parameter type for expanded function ((x$1) => add(1, x$1))        val addOne = add(1, _)                           ^
Heiko
Company: weiglewilczek.com
Blog: heikoseeberger.name
Follow me: twitter.com/hseeberger
OSGi on Scala: scalamodules.org
Lift, the simply functional web framework: liftweb.net
Luc Duponcheel
Joined: 2008-12-19,
User offline. Last seen 34 weeks 3 days ago.
Re: Why are type annotations necessary for leaving arguments o
Heiko,

the Scala type inferencer is (kind of) "unidirectional" flow-based

in contrast to
the Haskell type inferencer which is (kind of) "bidirectional" unification-based

you'll find longer answers in books or articles

you may think: what a pity,
but, do not forget, the type system of Scala
also has to take OO concepts into account
while Haskell is a (pure) functional language

Luc



On Tue, Mar 16, 2010 at 9:55 AM, Heiko Seeberger <heiko.seeberger@googlemail.com> wrote:
Hi,
Why is it necessary to use a type annotation when leaving an argument out? The compiler knows the type as you can take from here:
scala> val add = (x: Int, y: Int) => x + y add: (Int, Int) => Int = <function2>
scala> val addOne = add(1, _: String)<console>:5: error: type mismatch; found   : String required: Int        val addOne = add(1, _: String)
Why is the following not possible?
scala> val addOne = add(1, _)<console>:5: error: missing parameter type for expanded function ((x$1) => add(1, x$1))        val addOne = add(1, _)                           ^
Heiko
Company: weiglewilczek.com
Blog: heikoseeberger.name
Follow me: twitter.com/hseeberger
OSGi on Scala: scalamodules.org
Lift, the simply functional web framework: liftweb.net



--
  __~O
 -\ <,
(*)/ (*)

reality goes far beyond imagination

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Why are type annotations necessary for leaving arguments o

http://stackoverflow.com/questions/2363013/in-scala-why-cant-i-partially...

On Tue, Mar 16, 2010 at 9:55 AM, Heiko Seeberger
wrote:
> Hi,
> Why is it necessary to use a type annotation when leaving an argument out?
> The compiler knows the type as you can take from here:
> scala> val add = (x: Int, y: Int) => x + y
> add: (Int, Int) => Int =
> scala> val addOne = add(1, _: String)
> :5: error: type mismatch;
>  found   : String
>  required: Int
>        val addOne = add(1, _: String)
> Why is the following not possible?
> scala> val addOne = add(1, _)
> :5: error: missing parameter type for expanded function ((x$1) =>
> add(1, x$1))
>        val addOne = add(1, _)
>                            ^
> Heiko
> Company: weiglewilczek.com
> Blog: heikoseeberger.name
> Follow me: twitter.com/hseeberger
> OSGi on Scala: scalamodules.org
> Lift, the simply functional web framework: liftweb.net
>

Matthew Pocock 2
Joined: 2010-02-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Why are type annotations necessary for leaving arguments o
This continually bites me. It's very annoying. I recognise that there may be cases where type inference can't be done, but surely some cases are obvious? Perhaps a scala-compiler type inference guts person can explain the whys and wherefores?

Matthew

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland