On Thu, Sep 9, 2010 at 4:20 AM, "Martin J. Drst"
<duerst / it.aoyama.ac.jp> wrote:
> On 2010/09/08 23:44, Yukihiro Matsumoto wrote:
>> But just for curiosity, how do you define the semantics of x++ and
>> ++x, where you don't have explicit type for variables in Ruby.
>> For x += 1, it's easily defined as x = x + 1, but not that easy for
>> x++ and ++x.
>
> I don't understand how this relates to explicit types for variables. ++x is
> just x = x + 1. x++ is something like (x'=x, x = x+1, x'). Ruby doesn't have
> the C comma operator, and this can't implemented as a method, because we
> can't pass a variable by reference (the equivalent of &x in C) in Ruby. But
> this isn't relevant because like += and friends, ++ would just be syntactic
> sugar implemented by the interpreter, not overridable operators.

A key component to this ++ discussion is the atomicity of updates. x
+= 1 is obviously not atomic, since it expands to x = x + 1 (one read,
one increment, one write). In C/C++ and in Java, the increment
operation is still not atomic, since it will usually compile to the
same three operations, albeit using memory locations and registers. As
it stands, +=, ||=, and friends are easier to accept as non-atomic. ++
is very often perceived to be atomic when it is not, and so
introducing it would make discussions about atomicity more
complicated.

FWIW, though, I still like the idea of having ++ as sugar for += 1, if
only because it's two+ fewer characters to type.

In fact, if you slide the 1 over on top of the =, it almost looks like
++ (ok, it's more like + or +, but hey, -> doesn't *really* look
like a ).

- Charlie