Considering how often we do:
variable = variable + another;
variable = variable - another;
variable = variable * another;
variable = variable / another;
variable = variable & another;
variable = variable | another;
variable = variable ^ another;
we then made the following language shortcuts:
variable += another;
variable -= another;
variable *= another;
variable /= another;
variable &= another;
variable |= another;
variable ^= another;
so, then, considering how often we do this:
variable = variable.different();
// or variable = variable->different();
// or variable = variable::different();
(for instance, stringVar = stringVar.toUpperCase()
), why isn't there one of these?
variable .= different();
// or variable ->= different();
// or variable ::= different();
is it a parsing issue? Would it be too hard? Or is there some obvious thing I don't see here?
I'm not asking for opinions, here. I want to know if there's any documented, historic, technical, or other verifiable reasons for why there is no such operator.
variable = variable.different();
appear in code? $\endgroup$variable.set_different()
would make more sense? (Ifdifferent()
is only used for updating the calling object, then using the assignment operator may not be the best syntax anyway.) $\endgroup$x = x.m()
occurs in code, compared tox = x + y
/x += y
. It's possible that language designers thoughtx = x.m()
would be rare. In C, I would expect it is rare. But your example helps -- it suggests that this probably applies only to immutable types; for mutable types, we'd be more likely to use a different idiom, as Paul Clayton mentions. For instance, maybe the designers of C figured it'd be rare, and the other languages just copied from C. But this is speculation; I don't know how we'd ever get evidence. $\endgroup$