T
Tom Anderson
Not and kept it immutable. I’d probably have outlawed ++, +=, etc.
+1
Alternatively, i might do what Python does, and define a += b as shorthand
for a = a + b. Then ++a as a = a + 1.
tom
Not and kept it immutable. I’d probably have outlawed ++, +=, etc.
Lew said:Now, instead of piling on me like a rugby scrum, folks, consider if I'm
asking people to do something prudent or foolish. "Be careful,
autoboxing is dangerous." That communicates advice to research (why
should I suddenly be Professor Proof for all you lazy jerks?) and
*decide for yourself* what the limits of autoboxing are so that you are
not surprised.
You are absolutely correct. The example was trivial, not tied to
autoboxing particularly, and perfectly scaled for someone who was
apparently unwilling to do the research.
And certainly started a conversation that led to all sorts of useful
links, concrete and much, much better examples than mine, and hours of
entertainment. Oh, and utter vindication of my original, overstated point:
Autoboxing isn't all that useful. It has dangers. I prefer not to use it.
Eric Sosman wrote:
Re-write the code without auto-boxing/unboxing -- pretend you're
using a pre-1.5 Java -- and I'm sure you'll see the problem. (And
since you didn't see it right away, I think that lends support to the
notion that autoboxing is, or at any rate can be, "dangerous.")
That's just your opinion.
... and marked as such by the phrase "I think."[*] I lack the
arrogance to propound my opinions as "facts."
:-(
[*] René Descartes walks into a bar, and the bartender asks "The
usual?" Descartes says "I think not," and poof! he disappeared!
And I lack the political correctness to present facts as mere opinion.
There is nothing about "strange" or "surprising" in the definition of
"danger", just risk of harm. NPE is harm. Misuse of autoboxing can
cause that harm. Ergo, autoboxing is dangerous.
http://www.merriam-webster.com/dictionary/danger?show=0&t=1299099462
(If I really want closures, I can work in Scheme, or OCaml, or F#, or
any of the other dozens of functional languages I have lying around.
Or Clojure, I suppose, if I want to target the JVM. No need to make
Java be everything.)
On 3/3/11 9:46 AM, javax.swing.JSnarker wrote:
[...]
Some language features make it easier to do than others.
So?
So, that is the entire point of this discussion. Many programmers prefer
to avoid the use of language features when those language features offer
an above-average probability of writing code that looks correct but
which is not.
But where do you draw the line?
How is it that you don't already know the answer to that question? I
have already made clear statements explaining my position.
As I already told you, Java does not allow that. You even quoted the
relevant part of my post
C# doesn't have special auto-boxing destination types. Boxing of value
types always results in a reference to an object typed as System.Object.
More to the point, no matter what one tries to do by way of preventing
this error, the type presented to the "synchronized" statement can
always be made to be simply java.lang.Object, regardless of where that
object comes from. You can always turn the wrong thing into
java.lang.Object with a cast. So any compile-time restrictions are never
going to be more than just "rule of thumb" kinds of restrictions; it is
not literally possible for the compiler to _prevent_ these errors. It
can only make them more difficult.
While I feel it's poor practice to use "this" or other exposed
references for synchronization, _that_ is clearly not something the
language designers are concerned about and in fact clearly feel it
should be allowed. Given that, then to impose other kinds of syntactic
restrictions on the "synchronized" statement does little to _really_
prevent these errors from occuring; it just complicates the language
specification and the compiler, while forcing the inept and
inexperienced programmers to add casts to their code in order to make
their mistakes.
Sorry if I did not make my point clear. There are reasons for
immutability independent of auto-boxing. _Those_ reasons remain the
same.
public class Paraphrase
{
int counter = 0;
Object lock = new Object();
public void increment
{
synchronized (lock)
{
counter++;
}
}
}
Please go back an re-read the text.
We were specifically discussing Integer and "hypothetical new integer
types", _not_ AtomicInteger.
Second, by your reasoning, hypothetical new operator overloads have no
bearing on the discussion, making your entire line of reasoning irrelevant.
How in the world did you find the statement "Or an 'int' instead of an
'Integer'" to be argumentative?
Get a grip.
I would say that your signature calls for it.
Peter Duniho said:Java could have take a related (but not precisely the same) approach by
prohibiting the use of _boxed_ value types (e.g. java.lang.Integer) in a
"synchronized" statement. It already requires the expression in the
"synchronized" statement to be a reference, refusing to auto-box in the
same way that C# does. But it also could prohibit auto-box destination
types, such as java.lang.Integer.
[...]
Gratuitous rudeness on your part.
Um, it seems that your snarky predilection has left you a bit paranoid
about the motivations and behaviors of others.
What does your classic unsubstantiated and erroneous claim have to do
with Java, Vajhøj?
And it is very substantiated - everyone can read your signature.
Um, it seems that your snarky predilection has left you a bit [insult
deleted]. If you're going to go all Twisted/Jerry/Ken on me, [implied
insult deleted].
rossum said:Now where have I heard that before?
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.