What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?
Please only one feature per answer.
What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?
Please only one feature per answer.
Don't know if it can be considered a language feature, but, in C++ almost any compiler error related to templates delivers a fair amount of WTF to many C++ programmers around the world on daily basis :)
The JavaScript octal conversion 'feature' is a good one to know about:
More details here.
For those who don't know,
bc
is an "arbitrary precision calculator language", and I use it quite often for quick calculations, particularly when the numbers involved are large ($
is the prompt):bc
has been a standard Unix command for a long time.Now for the "WTF feature". This is from
man bc
(emphasis mine):JavaScript is object oriented, right? So running methods on literal strings and numbers should work. Like
"hello".toUpperCase()
and3.toString()
. Turns out that second one is a syntax error, why? Because the parser expects a number followed by a dot to be a floating point literal. That's not the WTF, the WTF is that you only have to add another dot to make it work:The reason is that the literal
3.
is interpreted as3.0
, and3.0.toString()
works fine.Fun with auto boxing and the integer cache in Java:
Explanation
A quick peek at the Java source code will turn up the following:
Note:
IntegerCache.high
defaults to127
unless set by a property.What happens with auto boxing is that both foo and bar the same integer object retrieved from the cache unless explicitly created: e.g.
foo = new Integer(42)
, thus when comparing reference equality, they will be true rather than false. The proper way of comparing Integer value is using.equals;
Duff's device in C!
In C one can interlace a do/while with a switch statement. Here an example of a memcpy using this method: