- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why are type annotations necessary for leaving arguments out?
Tue, 2010-03-16, 09:55
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
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
Tue, 2010-03-16, 10:37
#2
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
>
Tue, 2010-03-16, 13:57
#3
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
Matthew
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:
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination