Idea for Ruby 2.0

Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
something like this:

[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }

The idea is that I'm not interested in the second part of the internal
arrays, only the first. The lowline works, but obviously for one
parameter, as it is a valid identifier. I was thinking whether Ruby 2.0
should introduce I-don't-care-type variables, like Haskell or Prolog
has. It would make all identifiers beginning with a lowline invalid,
except in parameter-lists, which could mean too much incompatability
with Ruby 1.x, but perhaps it's worth at least some thought?,
        nikolai

···

--
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

Why would it make all identifiers beginning with an underscore
invalid? One don't care variable should be enough for everybody I
think ;-).

a, _, b, _, c = *function()

would make sense to me, though

a, _a, b, _b, c = *function()

could continue to set five variables.

Otherwise I like this at least on first sight. I indeed needed it for
some small scripts just a short while ago. There I used something like
my second example and simply ignored _a and _b, but the first example
would have been less intrusive to me.

regards,

Brian

···

On 28/07/05, Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
something like this:

[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }

The idea is that I'm not interested in the second part of the internal
arrays, only the first. The lowline works, but obviously for one
parameter, as it is a valid identifier. I was thinking whether Ruby 2.0
should introduce I-don't-care-type variables, like Haskell or Prolog
has. It would make all identifiers beginning with a lowline invalid,
except in parameter-lists, which could mean too much incompatability
with Ruby 1.x, but perhaps it's worth at least some thought?,
        nikolai

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

Hi,

···

In message "Re: Idea for Ruby 2.0" on Thu, 28 Jul 2005 19:03:43 +0900, Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> writes:

Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
something like this:

[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }

The idea is that I'm not interested in the second part of the internal
arrays, only the first.

How about

  [[1, 2], [2, 3], [3, 4]].each{ |first, *| fs << first }

that works _now_?

              matz.

Perl solves this by allowing you to assign to undef:

$ perl -e '($one, undef, $two) = split " ", "1 2 3"; print "$one $two\n"'
1 3

Just FYI.

James Edward Gray II

···

On Jul 28, 2005, at 5:03 AM, Nikolai Weibull wrote:

Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
something like this:

[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }

The idea is that I'm not interested in the second part of the internal
arrays, only the first.

Nikolai,

Lately I've found myself using pseudo-anonymous variables
a lot, e.g., something like this:

[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }

The idea is that I'm not interested in the second part of
the internal arrays, only the first. The lowline works,
but obviously for one parameter, as it is a
valid identifier.

Interestingly, blocks can have duplicate argument names,

   {1=>2}.each {|_,_| puts _} #=> 2

but methods can't:

   def foo _,_ ; end #=> SyntaxError

I was thinking whether Ruby 2.0 should introduce
I-don't-care-type variables, like Haskell or Prolog has.

As you said, it would be useful when *passing* arguments,
and it's obviously useful in blocks. I can see how it could
be worthwhile to allow it in method definitions too ---
for symmetry if nothing else.

It would make all identifiers beginning with a lowline
invalid, except in parameter-lists,

As others have pointed out, I'd think just one identifer
would need to be reserved: the single-underscore one.

which could mean too much incompatability with Ruby 1.x,
but perhaps it's worth at least some thought?

Even reserving just the single-underscore identifier would
mean some incompatibility, but then you *did* put "Ruby 2.0"
in the subject line. :slight_smile:

···

--
Daniel Brockman <daniel@brockman.se>

    So really, we all have to ask ourselves:
    Am I waiting for RMS to do this? --TTN.

Yukihiro Matsumoto wrote:

Hi,

>Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
>something like this:
>
>[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }
>
>The idea is that I'm not interested in the second part of the internal
>arrays, only the first.

How about

  [[1, 2], [2, 3], [3, 4]].each{ |first, *| fs << first }

that works _now_?

              matz.

Hello,

What about
[[1, 2, 3], [2, 3, 4], [3, 4, 5]].each{ |first, *, last| fs << first,
last }
?
It does not work (and should not, as now). The idea was to skip some
part of incoming data.

BTW is there any way to skip a parameter when a method is called wich
has default parameters, like:

def aMethod( a = 1, b = 2)
   #some stuff
end

aMethod( _, 4)# I do not want to change first parameter value and do
not know its default one.

BR,
Pavel

···

In message "Re: Idea for Ruby 2.0" > on Thu, 28 Jul 2005 19:03:43 +0900, Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> writes:

Brian Schröder wrote:

···

On 28/07/05, Nikolai Weibull > <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

> I was thinking whether Ruby 2.0 should introduce I-don't-care-type
> variables, like Haskell or Prolog has. It would make all
> identifiers beginning with a lowline invalid, except in
> parameter-lists, which could mean too much incompatability with Ruby
> 1.x, but perhaps it's worth at least some thought?,

Why would it make all identifiers beginning with an underscore
invalid? One don't care variable should be enough for everybody I
think ;-).

a, _, b, _, c = *function()

would make sense to me, though

a, _a, b, _b, c = *function()

could continue to set five variables.

Well, it depends on how you would want it. In Prolog (don't remember
how Haskell does it), all variable beginning with a lowline are ignored
- it adds some documentational value. Perhaps only _ is enough,
        nikolai

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

Yukihiro Matsumoto wrote:

> Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
> something like this:
>
> [[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }
>
> The idea is that I'm not interested in the second part of the internal
> arrays, only the first.

How about

  [[1, 2], [2, 3], [3, 4]].each{ |first, *| fs << first }

that works _now_?

Hm, yeah. :-). I had no idea, thanks. Still, it wouldn't currently
work in |a, *, c|,
        nikolai

···

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

Daniel Brockman wrote:

Interestingly, blocks can have duplicate argument names,
  {1=>2}.each {|_,_| puts _} #=> 2
but methods can't:
  def foo _,_ ; end #=> SyntaxError

That's because blocks don't actually have "arguments." They follow the rules of parallel assignment.
http://phrogz.net/ProgrammingRuby/language.html#blocksclosuresandprocobjects
http://phrogz.net/ProgrammingRuby/language.html#parallelassignment

Devin
I decided that I can only post one email per day to this list, so as not to be a total spammer. This is the one I chose. So sue me.

Maybe if I'm good, I'll up it to two.

Probably by next week, I'll have given up.

Daniel Brockman wrote:

As you said, it would be useful when *passing* arguments,
and it's obviously useful in blocks. I can see how it could
be worthwhile to allow it in method definitions too ---
for symmetry if nothing else.

  ^^^^^^^^^^^^

Precisely so.

> which could mean too much incompatability with Ruby 1.x,
> but perhaps it's worth at least some thought?

Even reserving just the single-underscore identifier would
mean some incompatibility, but then you *did* put "Ruby 2.0"
in the subject line. :slight_smile:

Yeah, still, perhaps it can be flexible enough to allow it as a valid
identifier if it is actually being used in the body of whatever's using
it...,
        nikolai

···

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

But only for the sake of documentational value it works right now out
of the box. Just ignore variables beginning with an _ in your program,
and you are done. Or is your aim to optimize the time the assignment
takes?

regards,

Brian

···

On 28/07/05, Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

Brian Schröder wrote:

> On 28/07/05, Nikolai Weibull > > <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

> > I was thinking whether Ruby 2.0 should introduce I-don't-care-type
> > variables, like Haskell or Prolog has. It would make all
> > identifiers beginning with a lowline invalid, except in
> > parameter-lists, which could mean too much incompatability with Ruby
> > 1.x, but perhaps it's worth at least some thought?,

> Why would it make all identifiers beginning with an underscore
> invalid? One don't care variable should be enough for everybody I
> think ;-).
>
> a, _, b, _, c = *function()
>
> would make sense to me, though
>
> a, _a, b, _b, c = *function()
>
> could continue to set five variables.

Well, it depends on how you would want it. In Prolog (don't remember
how Haskell does it), all variable beginning with a lowline are ignored
- it adds some documentational value. Perhaps only _ is enough,
        nikolai

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

Underscore works as it is, so { |a, _, c| ... } works.

T.

Nikolai Weibull wrote:

Well, it depends on how you would want it. In Prolog (don't remember
how Haskell does it), all variable beginning with a lowline are ignored
- it adds some documentational value. Perhaps only _ is enough,
       nikolai

Offtopic: I thought those are just any old variables in Prolog. I know for 25% certain SWI-Prolog uses variables starting as underscores as anonymous variables it creates when getting itself into an infinite loop fit :stuck_out_tongue_winking_eye:

David

Brian Schröder wrote:

But only for the sake of documentational value it works right now out
of the box. Just ignore variables beginning with an _ in your program,
and you are done. Or is your aim to optimize the time the assignment
takes?

Well, you can still reference them if you want to. I guess
optimization would be one goal. Perhaps I'm just clutching at straws,
        nikolai

···

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

bschroed@black:~/svn/diplomarbeit/text$ irb
irb(main):001:0> def four_values() [1,2,3,4] end
=> nil
irb(main):002:0> a, _, b, _ = *four_values
=> [1, 2, 3, 4]

Ooops, everythings already there. I thought you could not assign in
parallel twice to the same variable. It seems it ain't really parallel
:wink:

regards,

Brian

···

On 28/07/05, Trans <transfire@gmail.com> wrote:

Underscore works as it is, so { |a, _, c| ... } works.

T.

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

"Trans" <transfire@gmail.com> wrote...

Underscore works as it is, so { |a, _, c| ... } works.

So does this: { |a, _, _, d| ... }

This doesn't: def foo(a, _, _, d)
.... but why not just do this: def foo(a, b, c, d)

And if you're after the case of a method in a subclass wanting to keep a
superclass' default parameter, one option is using a hash with named
arguments. But in this case I do see your point, and have felt the need
myself. The traits library handles stuff like this, I think.

Cheers,
Dave

David Vallner <david@vallner.net> writes:

Nikolai Weibull wrote:

Well, it depends on how you would want it. In Prolog (don't remember
how Haskell does it), all variable beginning with a lowline are ignored
- it adds some documentational value. Perhaps only _ is enough,
       nikolai

Offtopic: I thought those are just any old variables in Prolog. I know
for 25% certain SWI-Prolog uses variables starting as underscores as
anonymous variables it creates when getting itself into an infinite
loop fit :stuck_out_tongue_winking_eye:

I'm not sure they are "old variables"... compare foo(A, A, B) and
foo(_, _, B). I don't think they share the same semantics.

···

David

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

--- Nikolai Weibull

···

<mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

Brian Schröder wrote:

> But only for the sake of documentational value it works
right now out
> of the box. Just ignore variables beginning with an _ in
your program,
> and you are done. Or is your aim to optimize the time the
assignment
> takes?

Well, you can still reference them if you want to. I guess
optimization would be one goal. Perhaps I'm just clutching
at straws,
        nikolai

There is no reason Ruby couldn't optimize for the case where a
variable is declared in the argument list and never used.

__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail

Dave Burt wrote:

This doesn't: def foo(a, _, _, d)
.... but why not just do this: def foo(a, b, c, d)

Yeah, I tried using it twice in a def but it didn't work, so I figured
it wouldn't work in a block parameter-list either, but it turns out that
it does. That was actually where I most wanted it.

Thank you all for your input,
        nikolai

···

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

Christian Neukirchen wrote:
[SWI-Prolog]
> I'm not sure they are "old variables"... compare foo(A, A, B) and
> foo(_, _, B). I don't think they share the same semantics.

You're right, the semantics of both constructs differs:

foo(bar,baz,42).
foo(bar,bar,42).
foo(baz,baz,42).

% E:/Dokumente/Studium/Passau/5. Semester/DeduktiveDB/PrologWorkspace/RubyNEwsgroup.pl compiled 0.00 sec, 944 bytes

Yes
3 ?- foo(A,A,C).

A = bar
C = 42 ;

A = baz
C = 42 ;

No

As you see in the above query Prolog unifies the query variable A with the same atom whereas the query

4 ?- foo(_,_,C).

yields

C = 42 ;

C = 42 ;

C = 42 ;

No

and therefore in this respect is equivalent to this query:

5 ?- foo(A,B,C).

A = bar
B = baz
C = 42 ;

A = bar
B = bar
C = 42 ;

A = baz
B = baz
C = 42 ;

No

However in the underscore version you don't get to see which atoms the variables got unified with.

In this example there is in effect no real query /condition/ when you write A,B,C or _,_,C. On the other hand the query :- foo(A,A,C) states that the first two parameters must hold the same value.

Although :- foo(A,B,C) and :- foo(_,_,C) have same query semantics the underscore version is a lot easier on the eye in my opinion. Writing 4711.times{ |_| puts "Ruby rocks" } seems odd to me, though.

Regards,
Matthias