On Fri, Oct 30, 2009 at 11:05 AM, Robert Klemme
<shortcutter / googlemail.com> wrote:
> 2009/10/30 RichardOnRails <RichardDummyMailbox58407 / uscomputergurus.com>:
>
>> I'll correct my errors and probably have a new theory subsequently.
>
> IMHO the story goes like this: absence of postfix ++ and -- is a
> consequence of the fact that numeric types are immutable in Ruby which
> makes an assignment necessary for these operators.  ¨Βμτθουητθατ
> would be doable, it would not immediately be obvious when looking at
> "foo++".  ¨Βτθοτθεσιδε’ζοο «½ ±’ ναλετθασσιηξνεξτ οβφιου> while still being pretty concise (you do not have to write "foo = foo
> + 1").

I think that theres a more fundamental problem with ++ in a language
like ruby, which has to do with the difference between objects and
variables.

The c ++ and -- operators change a variable, NOT a value.

so in C

  a = 1
  b = a
  a++

a is now 2, but b is still 1.

Now, consider not immutable objects, but defining ++ for a mutable
object.  I've named the method plus_plus instead of ++ since I can do
the former, but not the latter.

class String
  def plus_plus
    self << " plus a plus"
  end
end


a = "A non-plussed string"
b = a

puts "a is #{a.inspect}"
puts "b is #{b.inspect}"

a.plus_plus

puts "a is #{a.inspect}"
puts "b is #{b.inspect}"

When we run this we see that the result is:

a is "A non-plussed string"
b is "A non-plussed string"
a is "A non-plussed string plus a plus"
b is "A non-plussed string plus a plus"

Because in languages like Ruby with object reference variable
semantics, methods can only operate on objects, not the variables
which reference them.

So if you COULD successfully define Fixnum#++, or Fixnum#plus_plus:

a = 1
a++

could only change the singleton instance of 1 into 2, which probably
isn't something you'd really want to do.

By 'you' I'm not aiming at you Robert.

This reminds me of a very hard bug I encountered years ago when I was
first was learning to program in Fortran.  In the original Fortran you
could assign a new value to a subroutine parameter inside the
subroutine.  If the actual parameter value was an integer literal, you
could literally change 1 to 2, and that's why I know you probably
don't want to do this.
-- 
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