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

syntax/documentation issue/question

4 replies
Raoul Duke
Joined: 2009-01-05,
User offline. Last seen 42 years 45 weeks ago.

hi,

it seems like everywhere i look, there is almost always (hyperbole, a
bit) a different syntax claimed as the right one for doing anonymous
functions, so i think it is very confusing for a newbie to try to get
something working out of the box e.g. just in the repl. kinda
frustrating, kinda off-putting.

http://www.scala-lang.org/node/133 (found via googling for "scala
anonymous function")

vs. interactive scala
Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java
1.5.0_16).

scala> (x:Int) => x+1
res21: (Int) => Int =
// that was ok.

scala> (x:Int, y:Int) => x+y
:1: error: not a legal formal parameter
(x:Int, y:Int) => x+y
^
// that was not.

Johannes Rudolph
Joined: 2008-12-17,
User offline. Last seen 29 weeks 21 hours ago.
Re: syntax/documentation issue/question

On Wed, Mar 18, 2009 at 11:59 PM, Raoul Duke wrote:
> scala> (x:Int, y:Int) => x+y
> :1: error: not a legal formal parameter
> (x:Int, y:Int) => x+y
> ^
> // that was not.
>

In the standalone case, you have to put another set of parentheses
around the function definition:

scala> ((x:Int, y:Int) => x+y)
res0: (Int, Int) => Int =

If you assign it to a variable they are unnecessary:

scala> val f = (x:Int, y:Int) => x+y
f: (Int, Int) => Int =

Johannes

PS: I'm not sure why the parser makes this case special. Perhaps
because it is a rare use case to use anonymous functions as
statements?

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: syntax/documentation issue/question
maybe, but then why does a single parameter anonymous function pass?

Ishaaq

2009/3/19 Johannes Rudolph <johannes.rudolph@googlemail.com>
On Wed, Mar 18, 2009 at 11:59 PM, Raoul Duke <raould@gmail.com> wrote:
> scala> (x:Int, y:Int) => x+y
> <console>:1: error: not a legal formal parameter
> (x:Int, y:Int) => x+y
> ^
> // that was not.
>

In the standalone case, you have to put another set of parentheses
around the function definition:

scala> ((x:Int, y:Int) => x+y)
res0: (Int, Int) => Int = <function>

If you assign it to a variable they are unnecessary:

scala> val f = (x:Int, y:Int) => x+y
f: (Int, Int) => Int = <function>

Johannes

PS: I'm not sure why the parser makes this case special. Perhaps
because it is a rare use case to use anonymous functions as
statements?

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: syntax/documentation issue/question

On Thu, Mar 19, 2009 at 07:31:44AM +0100, Johannes Rudolph wrote:
> In the standalone case, you have to put another set of parentheses
> around the function definition:
>
> scala> ((x:Int, y:Int) => x+y)
> res0: (Int, Int) => Int =

Or, use trunk! I have fixed several parsing issues with function
definitions.

scala> (x:Int, y:Int) => x+y
res1: (Int, Int) => Int =

Raoul Duke
Joined: 2009-01-05,
User offline. Last seen 42 years 45 weeks ago.
Re: syntax/documentation issue/question

> Or, use trunk! I have fixed several parsing issues with function
> definitions.

cool! looking forward to the next official release then ;-)

sincerely.

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