Ruby doesn't implement x++ for Fixnum's because ???

T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

As Matz himself has pointed out in this thread,


Which is something I've said on this thread before (multiple times IIRC).

This has nothing to do with whether or not the object bound to a
variable is immutable, it has to do with how ruby variable bindings
can and cannot be changed, and that is the whole point.

You still seem to be missing what I'm proposing.

For Numerics, ++ would rebind. For everything else, it would be dispatched
as a message.

Am I being unclear?
 
M

Marnen Laibow-Koser

Tony said:
You still seem to be missing what I'm proposing.

For Numerics, ++ would rebind. For everything else, it would be
dispatched
as a message.

Yuuuuuuuck! Why do this ugly special-casing for something that's hardly
ever needed anyway?
Am I being unclear?

No, just silly. :D

Best,
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

Yuuuuuuuck! Why do this ugly special-casing for something that's hardly
ever needed anyway?

To reiterate from my previous message, because the behavior of Numerics is
already a special case to begin with.
 
W

Walton Hoops

-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]] On Behalf Of Tony
To reiterate from my previous message, because the behavior of Numerics
is already a special case to begin with.

How? Because they are immutable? That's not special casing, that's just
how the class is designed. I can write an immutable class in Ruby,
without any special casing.
 
S

Seebs

You still seem to be missing what I'm proposing.
For Numerics, ++ would rebind. For everything else, it would be dispatched
as a message.
Am I being unclear?

I don't think that gives the right semantics in many cases. It's also
not clear that rebinding works:

array_example.length++

What should this do?

-s
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

No, but you ARE missing the fact that the lack of being able to rebind
a variable via a method has NOTHING to do with the class of the object
which is currently bound to that variable.

But the rebinding is being done by an operator, not a method, and there's
ample precedent for operators that perform rebinding in Ruby (=, +=, -=, /=,
etc)
 
S

Seebs

But the rebinding is being done by an operator, not a method, and there's
ample precedent for operators that perform rebinding in Ruby (=, +=, -=, /=,
etc)

And which of those operators special-case Numeric?

Any of them? I don't think so.

If it's to be a rebinding operator, it ought to rebind for everything, not
just for numerics.

-s
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

How? Because they are immutable? That's not special casing, that's just
how the class is designed. I can write an immutable class in Ruby,
without any special casing.

Yes, non-Numeric objects can be immutable. However, Numeric objects can't
be mutable.
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

I don't think that gives the right semantics in many cases. It's also
not clear that rebinding works:

array_example.length++

What should this do?

Well, this is a very interesting question, as I discovered something about
Ruby I didn't know from this...

Say we have:

class Foo
attr_reader :bar

def initialize
@bar = 0
end
end

f = Foo.new
f.bar += 1

What do you think the value of a subsequent call to f.bar will be?

I was surprised to discover that it indeed 1. Somehow += is mutating the
ivar through a supposed "attr_reader" even though there is no corresponding
bar= method. In that case += appears to be frobbing the ivar directly.

Very strange. Even worse:

class Foo
def initialize
@bar = 0
end

def bar
@bar + 1
end
end

f = Foo.new
f.bar += 1

Now what do you think the value of a subsequent call to f.bar will be?

Indeed, it would be 3!

I cannot begin to answer this question because Ruby is doing strange and
unexpected things here, at least from my perspective...
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

I cannot begin to answer this question because Ruby is doing strange and
unexpected things here, at least from my perspective...

Never mind, bar= was still defined because I was reopening the class.

So to answer your question:

array_example.length++

...would attempt to rebind through #length=, and fail if it weren't defined.
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

And which of those operators special-case Numeric?

Any of them? I don't think so.

Admittedly it would be a first.

If it's to be a rebinding operator, it ought to rebind for everything, not
just for numerics.


To borrow a phrase from 37signals, "context is more important than
consistency"
 
W

Walton Hoops

From: (e-mail address removed) [mailto:[email protected]] On Behalf Of Tony
Yes, non-Numeric objects can be immutable. However, Numeric objects
can't
be mutable.

Incorrect. In fact the very first message in this thread provided an
example of redifining Fixnum in such a way that it was mutable.
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

Incorrect. In fact the very first message in this thread provided an
example of redifining Fixnum in such a way that it was mutable.

No it didn't, that post created a new class called "FixNum"

And again, that's beside the point, which is to allow the expected "++"
semantics for immutable Numerics while also allowing in-place incrementation
for other classes which may store a mutable value, such as counters (whose
backing store may be a database, memcache, etc)

That would provide a truly Ruby-like ++ experience, aside from the fact you
can't redefine #++ for a Numeric, which probably isn't a good idea anyway.
 
W

Walton Hoops

-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]] On Behalf Of Tony
No it didn't, that post created a new class called "FixNum"

I apologize, you are correct that post defined a new class, due to a
capitalization error, which was corrected a few posts down.

The point is, yes indeed, numerics can be mutable. I could if I were
so inclined write a String backed subclass of Numeric which represtend
integers as Roman Numerals and was mutable, (or Fixnum backed if we're
feeling boring).
And again, that's beside the point, which is to allow the expected "++"
semantics for immutable Numerics while also allowing in-place
incrementation
for other classes which may store a mutable value, such as counters
(whosebacking store may be a database, memcache, etc)

But your missing the point that ++ is NOT expected. Not in Ruby. Ruby
simply doesn't work that way. You talked about "operator rebinding",
but you ignored the fact that ++ is nothing like those examples.

a+=<expression> expands to a=a+<expression>. It's a simple syntaxtic
sugar. a++ expands to... what? Nothing. There is no way you could
expand puts foo(a++,b) and get the desired behavior.

This discussion has gone on and on, and it's really quite pointless.
There is no good reason to include ++ in Ruby, and every reason not to.
No one can even agree on what they would expect a ++ operator to do when
used "The Ruby Way", with C programmers expecting it to work like C, and
the Rubyists expecting it to follow the Ruby way, with every operator
being a method call on that object (NOT variable).

So why introduce this operator into the language that would cause all
this confusion, when all it will do is save a few C programmers a SINGLE
line of code. 95% of the time it won't even do that, if they would
learn and do things the Ruby way, rather than trying to write C code in
Ruby.
 
F

Florian Aßmann

Gaah, close this thread! ;)

Cheers
Florian

Am 08.11.2009 um 21:26 schrieb Tony Arcieri:
 
M

Marnen Laibow-Koser

Florian said:
Gaah, close this thread! ;)

I don't think there's a mechanism for doing that, since this is an
unmoderated list. You don't have to follow it if you don't want to. :)
Cheers
Florian

Am 08.11.2009 um 21:26 schrieb Tony Arcieri:

Best,
 
T

Tony Arcieri

Gaah, close this thread! ;)

I suppose the whole discussion is moot as Ruby will likely never see a ++
operator.

I was just trying to make clear the limitation wasn't a technical one, and
further show how a ++ operator could be "Ruby-like" while still retaining
C/C++/Java-like semantics.

--=20
Tony Arcieri
Medioh/Nagravision
 
M

Marnen Laibow-Koser

Tony said:
I suppose the whole discussion is moot as Ruby will likely never see a
++
operator.

I was just trying to make clear the limitation wasn't a technical one,
and
further show how a ++ operator could be "Ruby-like" while still
retaining
C/C++/Java-like semantics.

And as others have made clear, you are incorrect.

Best,
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

And as others have made clear, you are incorrect.

What exactly is it you think I'm "incorrect" about?
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top