Fabulous Adventures In Coding
Eric Lippert is a principal developer on the C# compiler team. Learn more about Eric.
My sadly soon-to-be-erstwhile coworker Cyrus made me a lolgeek shirt to go with this series of blog articles:
Cyrus, needless to say, is a big goof. Thanks, dude!
Is it wrong that I love this sort of thing? Alas, I'm a nerd, through and through.
But I must say that i++ spoils all the fun. We are talking about immutable data structures and it mutates 'i'.
Yeah, stupid i++ !
Well, the object held in "i" isn't mutated (probably, we can't be sure because maybe "i" isn't an int and there is some funky operator overloading going on) the variable "i" is just set to something else.
Trust your instinct. I am not a compiler who is paranoid of even ++ and the language ain't C# either where overloading + automatcially gives you cousins for free. I must assume that ++ mutates the object it is invoked on.
I am not following the train of thought of either of you.
If i contains an int then the int is not mutated; ints are immutable.
The operand of the ++ operator is always a variable. No matter what the type of the variable is, and no matter what is presently stored in that variable, a successful ++ operation always mutates _the variable_. Whether the contents of the variable are mutable or immutable is irrelevant; the variable is going to change.
(Well, someone could write a ++ operator which set the variable to its current value, not mutating it, but we could characterize the trivial mutation as a mutation for the sake of argument.)
Hence the shirt -- i'm mutating your variables.
alright... that was deep;
i now fully appreciate that shirt... as obvious to before you explained it... i didn't get it :]
lol.... I must of re-read your explaination at least 4 times before getting it though;
O.K. The shirt is fun but a variable is not in the realm of Data Structures!
In this context 'i++' can only mean a better 'i'mmutable Data Structure! ( as C++ means a better C )
So keep up the good work,
For some reason, there's been a lot of buzz lately around immutability in C#. If you're interested in
The mark of a true geek. See a joke about a computer science concept and then dissect it to see if it's really funny.