- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
syntax/documentation issue/question
Wed, 2009-03-18, 23:59
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.
Thu, 2009-03-19, 09:17
#2
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>
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
Thu, 2009-03-19, 13:07
#3
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 =
Thu, 2009-03-19, 17:37
#4
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.
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