R
Robert Klemme
2009/11/4 lith said:Wouldn't it be cool if ruby had macros?
No.
2009/11/4 lith said:Wouldn't it be cool if ruby had macros?
There's really no reason it shouldn't be added [...]
I have seen no convincing argument as to why ++ is not supported in Ruby.
Of course I had to jump in here.
Yes, a++ and ++a could easily be rewritten by the parser into the
appropriate increment+set of a and the expression either returns the
incremented value or the non-incremented value. And I would like to
see that added. It doesn't fundamentally change the expectations of
the programmer, and it provides a one-character-shorter version of
a+=1. There's really no reason it shouldn't be added, because even in
Java or C, you are *never* modifying arbitrary references to that
value...you are *always* re-assigning the value a given variable
points to.
This example:
a = 1
b = a
a++
Would cause exactly the same results in every language I've worked
with...b would be 1 and a would be 2. The ++ operator never modifies a
value, it modifies what value the variable has assigned to it. If it
were modifying a value, then using ++ to bump a pointer through memory
offsets would have horrible side effects for anyone else assigned that
pointer value.
I have seen no convincing argument as to why ++ is not supported in Ruby.
2009/11/4 David A. Black said:Hi --
It would, I think, be quite anomalous, since it would be the only case
(that I can think of anyway) of an assignment expression that didn't
look and feel like an assignment expression.
I'm also not sure what problem it would be solving, other than adding
to the "make <language> users feel at home in Ruby" effect. But I tend
to think that Ruby should move away from, not towards, doing things
for exclusively that reason.
Of course I had to jump in here.
Yes, a++ and ++a could easily be rewritten by the parser into the
appropriate increment+set of a and the expression either returns the
incremented value or the non-incremented value. And I would like to
see that added. It doesn't fundamentally change the expectations of
the programmer, and it provides a one-character-shorter version of
a+=1. There's really no reason it shouldn't be added, because even in
Java or C, you are *never* modifying arbitrary references to that
value...you are *always* re-assigning the value a given variable
points to.
This example:
a = 1
b = a
a++
Would cause exactly the same results in every language I've worked
with...b would be 1 and a would be 2. The ++ operator never modifies a
value, it modifies what value the variable has assigned to it. If it
were modifying a value, then using ++ to bump a pointer through memory
offsets would have horrible side effects for anyone else assigned that
pointer value.
I have seen no convincing argument as to why ++ is not supported in Ruby.
Certainly it could be implemented in an extension to the language as
syntactic sugar much as += and it's family.
But I maintain, that it can't be implemented as a method, any more
than += or ||= could be.
If Matz deigned to do such a language change, I'd certainly feel free
to ignore it. <G>
I can't help but think of Alan Perlis' quip that "syntactic sugar
causes cancer of the semicolons"
--
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
I believe you are quite wrong. If a destructive function like gsub! can
be implemented as a method, then I see no reason that +=, |=, or postfix
++ couldn't be.
which is separate from the object itself. =A0Fixnum has no such storage t= o
refer to.
I don't think ruby makes the distinction between native types &
objects à la java.
You've got the onus the wrong way around.
And adding C-like operators based on a partiular assembly language to
any 21st century language, especially a high-level one, just seems
absurd!
Extra documentation so we can save three characters (a++ instead of a
+= 1) in a rare use case? No thanks!
The ++ operator, far more than just being syntactic sugar for +=1, would
allow you to send an "increment" message to any object, which would change
its value in place, i.e.
Hi --
It would, I think, be quite anomalous, since it would be the only case
(that I can think of anyway) of an assignment expression that didn't
look and feel like an assignment expression.
I'm also not sure what problem it would be solving, other than adding
to the "make <language> users feel at home in Ruby" effect. But I tend
to think that Ruby should move away from, not towards, doing things
for exclusively that reason.
David
--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com
David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
Hi --
It would, I think, be quite anomalous, since it would be the only case
(that I can think of anyway) of an assignment expression that didn't
look and feel like an assignment expression.
I'm also not sure what problem it would be solving, other than adding
to the "make <language> users feel at home in Ruby" effect. But I tend
to think that Ruby should move away from, not towards, doing things
for exclusively that reason.
David
And how exactly would you change the value of 1 in place?
And how exactly would you change the value of 1 in place?
You don't. =A0Are you insinuating the behavior of Fixnums isn't already
special cased to begin with?
a =3D 1
a.class #Fixnum
a++ # a is now 2
1.class #Fixnum
1++ # Illegal
So although a is a Fixnum, and 1 is a Fixnum, they respond to ++ differentl=
y?
--=20
Paul Smith
http://www.nomadicfun.co.uk
(e-mail address removed)
gsub! is implemented as a method on objects which contain data. ++ would
have to be implemented as a method on objects which ARE their data -- which
have no distinction between the object and its "contents".
gsub! can work because somewhere inside the object there is a hunk of storage
which is separate from the object itself. Fixnum has no such storage to
refer to.
-s
class Fixnum
def pp # We can?t define ++ because of a compiler restriction.
self + 1
end
end
Appending these lines shows that a & b values are distinct.
That is, after incrementing, a =2, b is unchanged; b is not
impacted by a?s change
a += 1; show (a) => Got 2; class = Fixnum; object_id = 5; v >> 1
= 2
show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1
Appending these lines shows the ++?s alias pp works just find
a=1; show(a.pp) => Got 2; class = Fixnum; object_id = 5; v >> 1
= 2
show(b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1
Do you agree?
From: (e-mail address removed) [mailto:[email protected]] On Behalf Of Tony
Arcieri
wrote:
I think you're missing why ++ could be useful, and it's precisely
because
Ruby is a "21st century language"
The ++ operator, far more than just being syntactic sugar for +=1,
would
allow you to send an "increment" message to any object, which would
change
its value in place, i.e.
def ++
incrementing_logic_goes_here
end
I could see this as being handy
You don't. =A0Are you insinuating the behavior of Fixnums isn't already
special cased to begin with?
yes, i see your point, but consider:
a =3D Foo.new()
b =3D a
c =3D 42
d =3D c
a++
p b
c++
p d
martin
RichardOnRails said:BTW, I'm not advocating x++ for Ruby. I'm just trying to understand
whether Ruby would literally change 1 to 2 as opposed to change a
variable that contains 1 to subsequently contain 2.
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.