Default argument values for blocks

D

Daniel Schierbeck

Is there a reason why I can't do this?

foo = lambda { |foo = bar| puts foo }
foo.call

I can't think of any good reason why this isn't valid.


Cheers,
Daniel Schierbeck
 
D

Daniel Schierbeck

Daniel said:
Is there a reason why I can't do this?

foo = lambda { |foo = bar| puts foo }
foo.call

I can't think of any good reason why this isn't valid.


Cheers,
Daniel Schierbeck

I of course meant |foo = "bar"|

Sorry
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Default argument values for blocks"

|Is there a reason why I can't do this?
|
| foo = lambda { |foo = bar| puts foo }
| foo.call
|
|I can't think of any good reason why this isn't valid.

Mostly because yacc does not allow it. It confuses

lambda { |foo = bar| puts foo }

as

lambda { |foo = (bar| puts foo) }

and causes syntax error.

matz.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Default argument values for blocks"

||Is there a reason why I can't do this?
||
|| foo = lambda { |foo = bar| puts foo }
|| foo.call
||
||I can't think of any good reason why this isn't valid.
|
|Mostly because yacc does not allow it. It confuses
|
| lambda { |foo = bar| puts foo }
|
|as
|
| lambda { |foo = (bar| puts foo) }
|
|and causes syntax error.

For your information, you can do

foo = ->(foo="bar"){puts foo}
foo.call

in 1.9.

matz.
 
A

Ara.T.Howard

Hi,

In message "Re: Default argument values for blocks"

||Is there a reason why I can't do this?
||
|| foo = lambda { |foo = bar| puts foo }
|| foo.call
||
||I can't think of any good reason why this isn't valid.
|
|Mostly because yacc does not allow it. It confuses
|
| lambda { |foo = bar| puts foo }
|
|as
|
| lambda { |foo = (bar| puts foo) }
|
|and causes syntax error.

For your information, you can do

foo = ->(foo="bar"){puts foo}
foo.call

in 1.9.

-> means lambda?

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
D

Daniel Schierbeck

Yukihiro said:
Hi,

In message "Re: Default argument values for blocks"

||Is there a reason why I can't do this?
||
|| foo = lambda { |foo = bar| puts foo }
|| foo.call
||
||I can't think of any good reason why this isn't valid.
|
|Mostly because yacc does not allow it. It confuses
|
| lambda { |foo = bar| puts foo }
|
|as
|
| lambda { |foo = (bar| puts foo) }
|
|and causes syntax error.

For your information, you can do

foo = ->(foo="bar"){puts foo}
foo.call

in 1.9.

matz.

Cool. Is "->" the same as "lambda"? Can you write this:

collection.each -> (element) { puts element }
 
K

Kevin Ballard

Yukihiro said:
Mostly because yacc does not allow it. It confuses

lambda { |foo = bar| puts foo }

as

lambda { |foo = (bar| puts foo) }

and causes syntax error.

yacc? Maybe I was informed wrong, but I was under the impression that
Ruby used a hand-rolled parser.
 
D

Daniel Berger

Yukihiro said:
Hi,

In message "Re: Default argument values for blocks"

||Is there a reason why I can't do this?
||
|| foo = lambda { |foo = bar| puts foo }
|| foo.call
||
||I can't think of any good reason why this isn't valid.
|
|Mostly because yacc does not allow it. It confuses
|
| lambda { |foo = bar| puts foo }
|
|as
|
| lambda { |foo = (bar| puts foo) }
|
|and causes syntax error.

For your information, you can do

foo = ->(foo="bar"){puts foo}
foo.call

in 1.9.

matz.

MY EYES!!!!

Please, no.

Dan
 
D

David A. Black

Hi --

Hi,

In message "Re: Default argument values for blocks"

||Is there a reason why I can't do this?
||
|| foo = lambda { |foo = bar| puts foo }
|| foo.call
||
||I can't think of any good reason why this isn't valid.
|
|Mostly because yacc does not allow it. It confuses
|
| lambda { |foo = bar| puts foo }
|
|as
|
| lambda { |foo = (bar| puts foo) }
|
|and causes syntax error.

For your information, you can do

foo = ->(foo="bar"){puts foo}
foo.call

in 1.9.

Is this definitely going to remain? (Please say no.... :)


David
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Default argument values for blocks"

|> foo = ->(foo="bar"){puts foo}

|-> means lambda?

Yes, as in Perl6. "lambda" here means lambda in other languages, not
the lambda method in Ruby.

matz.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Default argument values for blocks"

|> foo = ->(foo="bar"){puts foo}

|Is this definitely going to remain? (Please say no.... :)

It is, but appearance may be changed. But I haven't got the better
one.

matz.
 
E

Eric Mahurin

--- Yukihiro Matsumoto said:
Hi,
=20
In message "Re: Default argument values for blocks"
on Wed, 12 Oct 2005 02:40:48 +0900, "Ara.T.Howard"
=20
|> foo =3D ->(foo=3D"bar"){puts foo}
=20
|-> means lambda?
=20
Yes, as in Perl6. "lambda" here means lambda in other
languages, not
the lambda method in Ruby.
=20
matz.

Other than syntax, what's the difference between this and the
Proc that comes from the lambda method? Maybe how return,
break, etc is handled?

In addition to handling defaults in arguments, I think these
issues need to be addressed in blocks/lambdas:

- all variables in the defining scope of a block/lambda are
prevented from being GCed by the block/lambda (because of the
#binding). This can easily result in a leaky program if not
careful. I discussed several solutions to this in a previous
thread.

- a facility for making variables local (regardless of whether
a variable of the same name exists in the defining scope).=20
This would also be good to have for a begin-end type block (I'd
like to have it for making inlined macros where I don't want to
worry about what variables are already used). I discussed
several solutions for this in another thread.

Also, I saw some slides for ruby 2 back in 2003 showing that
arguments will always be local to the block and other variables
in the block will be in the same scope as the defining scope.=20
I think always making arguments local is a good thing, but
never localizing the other variables will be problematic if
another local variable facility is not provided.




=09
=09
__________________________________=20
Yahoo! Mail - PC Magazine Editors' Choice 2005=20
http://mail.yahoo.com
 
D

David A. Black

Hi --

Hi,

In message "Re: Default argument values for blocks"

|> foo = ->(foo="bar"){puts foo}

|Is this definitely going to remain? (Please say no.... :)

It is, but appearance may be changed. But I haven't got the better
one.

I definitely think a keyword would be better than -> .


David
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Default argument values for blocks"

|Other than syntax, what's the difference between this and the
|Proc that comes from the lambda method? Maybe how return,
|break, etc is handled?

They are same, same class, same behavior, except for full argument
syntax of method arguments.

|In addition to handling defaults in arguments, I think these
|issues need to be addressed in blocks/lambdas:
|
|- all variables in the defining scope of a block/lambda are
|prevented from being GCed by the block/lambda (because of the
|#binding). This can easily result in a leaky program if not
|careful. I discussed several solutions to this in a previous
|thread.

I think this issue should _not_ be solved by making new lambda-like
object, but something like making eval a keyword, so that non
referenced local variable can be erased unless eval is called within
the scope.

|- a facility for making variables local (regardless of whether
|a variable of the same name exists in the defining scope).
|This would also be good to have for a begin-end type block (I'd
|like to have it for making inlined macros where I don't want to
|worry about what variables are already used). I discussed
|several solutions for this in another thread.

In 1.9, you can declare block local variables explicitly, by the
syntax:

foo {|a; b, c| ...} # b and c are block local

so that

b=1
5.times{|i;b|
b=5 # modifying in-block "b"
p [i,b]
}
p b # outer "b" remain unchanged

|Also, I saw some slides for ruby 2 back in 2003 showing that
|arguments will always be local to the block and other variables
|in the block will be in the same scope as the defining scope.
|I think always making arguments local is a good thing, but
|never localizing the other variables will be problematic if
|another local variable facility is not provided.

See above.

matz.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Default argument values for blocks"

|I definitely think a keyword would be better than -> .

Maybe. Some prefer verbosity to clarity. So what keyword you think
"the best"?

matz.
 
E

ES

Yukihiro said:
Hi,

In message "Re: Default argument values for blocks"

|> foo = ->(foo="bar"){puts foo}

|Is this definitely going to remain? (Please say no.... :)

It is, but appearance may be changed. But I haven't got the better
one.

What about either

block = lambda(foo = 'bar') { puts foo } # Implicit

or

block = lambda(foo = 'bar') {|foo| puts foo } # Explicit

Or is -> solving an entirely different problem? (If so, would
you mind stating the problem again or pointing me to an earlier
discussion?)

E
 
A

Ara.T.Howard

Hi,

In message "Re: Default argument values for blocks"

|> foo = ->(foo="bar"){puts foo}

|-> means lambda?

Yes, as in Perl6. "lambda" here means lambda in other languages, not
the lambda method in Ruby.

by that you mean it is a 'real' method - not a proc object?

i like -> btw. though =>> has a bit more 'weight'.

regards.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Default argument values for blocks"

|> |-> means lambda?
|>
|> Yes, as in Perl6. "lambda" here means lambda in other languages, not
|> the lambda method in Ruby.
|
|by that you mean it is a 'real' method - not a proc object?

By that I mean, it is a syntax, which gives you a Proc object.

|i like -> btw. though =>> has a bit more 'weight'.

It depends on your taste.

matz.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Default argument values for blocks"

|Cool. Is "->" the same as "lambda"? Can you write this:
|
| collection.each -> (element) { puts element }

Yes. It is still experimental though.

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top