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.
In C, arrays can be indexed like so:
a[10]
which is very common.
However, the lesser known form (which really does work!) is:
10[a]
which means the same as the above.
In JavaScript:
'5' + 3 gives '53'
Whereas
'5' - 3 gives 2
In JavaScript, the following construct
return
{
id : 1234,
title : 'Tony the Pony'
};
returns is a syntax error due to the sneaky implicit semicolon insertion on the newline after undefined
return
. The following works as you would expect though:
return {
id : 1234,
title : 'Tony the Pony'
};
Even worse, this one works as well (in Chrome, at least):
return /*
*/{
id : 1234,
title : 'Tony the Pony'
};
Here's a variant of the same issue that does not yield a syntax error, just silently fails:
return
2 + 2;
JavaScript truth table:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
" \t\r\n" == 0 // true
Source: Doug Crockford
==
serve in the eyes of the language designer?
==
had the meaning of ===
, and then there was another operator, something like ~=
that allowed type coercion.
Trigraphs in C and C++.
int main() {
printf("LOL??!");
}
This will print LOL|
, because the trigraph ??!
is converted to |
.
Fun with auto boxing and the integer cache in Java:
Integer foo = 1000;
Integer bar = 1000;
foo <= bar; // true
foo >= bar; // true
foo == bar; // false
//However, if the values of foo and bar are between 127 and -128 (inclusive)
//the behaviour changes:
Integer foo = 42;
Integer bar = 42;
foo <= bar; // true
foo >= bar; // true
foo == bar; // true
A quick peek at the Java source code will turn up the following:
/**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
Note: IntegerCache.high
defaults to 127
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;
IntegerCache.high
, but only 1 line ahead, they decide it's better to hardcode the 128 (instead of using IntegerCache.high+1).
Commented
Jan 6, 2010 at 1:59
Quoting Neil Fraser (look at the end of that page),
try {
return true;
} finally {
return false;
}
(in Java, but behaviour is apparently the same in JavaScript and Python). The result is left as an exercise to the reader.
EDITED: As long as we are on the subject consider also this:
try {
throw new AssertionError();
} finally {
return false;
}
Control cannot leave the body of a finally clause
Commented
Jan 5, 2010 at 16:40
return
in finally
clause.
finally
what would the following code do: bool x = true; try { return x; } finally { x = false; }
Commented
Jan 11, 2010 at 6:08
APL (other than ALL of it), the ability to write any program in just one line.
e.g. Conway's Game of Life in one line in APL:
alt text http://catpad.net/michael/APLLife.gif
If that line isn't WTF, then nothing is!
And here is a video
The weird things C++ templates can be used for, best demonstrated by "Multi-Dimensional Analog Literals" which uses templates to compute the area of "drawn" shapes. The following code is valid C++ for a 3x3 rectangle
#include"analogliterals.hpp"
using namespace analog_literals::symbols;
unsigned int c = ( o-----o
| !
! !
! !
o-----o ).area;
Or, another example with a 3D cube:
assert( ( o-------------o
|L \
| L \
| L \
| o-------------o
| ! !
! ! !
o | !
L | !
L | !
L| !
o-------------o ).volume == ( o-------------o
| !
! !
! !
o-------------o ).area * int(I-------------I) );
Perl’s many built-in variables:
$#
— not a comment!$0
, $$
, and $?
— just like the shell variables by the same name$ˋ
, $&
, and $'
— weird matching variables$"
and $,
— weird variables for list- and output-field-separators$!
— like errno
as a number but strerror(errno)
as a string$_
— the stealth variable, always used and never seen$#_
— index number of the last subroutine argument... maybe@_
— the (non)names of the current function... maybe$@
— the last-raised exception%::
— the symbol table$:
, $^
, $~
, $-
, and $=
— something to do with output formats$.
and $%
— input line number, output page number$/
and $\
— input and output record separators$|
— output buffering controller$[
— change your array base from 0-based to 1-based to 42-based: WHEEE!$}
— nothing at all, oddly enough!$<
, $>
, $(
, $)
— real and effective UIDs and GIDs@ISA
— names of current package’s direct superclasses$^T
— script start-up time in epoch seconds$^O
— current operating system name$^V
— what version of Perl this isThere’s a lot more where those came from. Read the complete list here.
perldoc perlvar
every five seconds. (Though I confess that half the time I check it thinking "I know there's a special variable that can do this for me, I just don't remember which one..." =P )
Commented
Jan 4, 2010 at 7:26
use English;
is that it affects RegExp performance. I am not making this up. perldoc.perl.org/English.html#PERFORMANCE
Commented
Jan 4, 2010 at 13:06
/$foo[bar]/
, is the [bar]
part a character class or a subscript to the array @foo
? Grep perldata for the terrifying answer.
Commented
Jan 31, 2010 at 8:19
PHP's handling of numeric values in strings. See this previous answer to a different question for full details but, in short:
"01a4" != "001a4"
If you have two strings that contain a different number of characters, they can’t be considered equal. The leading zeros are important because these are strings not numbers.
"01e4" == "001e4"
PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates "01e4" == "10000"
as true because these are numbers with equivalent values. This is documented behaviour, it’s just not very sensible.
The JavaScript octal conversion 'feature' is a good one to know about:
parseInt('06') // 6
parseInt('07') // 7
parseInt('08') // 0
parseInt('09') // 0
parseInt('10') // 10
More details here.
Let's have a vote for all languages (such as PL/I) that tried to do away with reserved words.
Where else could you legally write such amusing expressions as:
IF IF THEN THEN = ELSE ELSE ELSE = THEN
(IF
, THEN
, ELSE
are variable names)
or
IF IF THEN THEN ELSE ELSE
(IF
is a variable, THEN
and ELSE
are subroutines)
IF(
could mean either the start of one of the varieties of IF, or an assignment to the IF array.
Commented
Feb 18, 2010 at 18:36
In C one can interlace a do/while with a switch statement. Here an example of a memcpy using this method:
void duff_memcpy( char* to, char* from, size_t count ) {
size_t n = (count+7)/8;
switch( count%8 ) {
case 0: do{ *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
}while(--n>0);
}
}
while
at the end is a (conditional) JMP
back to the do
, which explains why you can skip the do
and still end up in the loop.
Algol pass by name (illustrated using C syntax):
int a[3] = { 1, 2, 3 };
int i = 1;
void f(int j)
{
int k;
k = j; // k = 2
i = 0;
k = j; // k = 1 (!?!)
}
int main()
{
f(a[i]);
}
... template<typename T> struct by_name { virtual operator T&() = 0; }; void f(by_name<int> j) { ... } int main() { f(struct : by_name<int> { operator int&() { return a[i]; } }); }
?
Commented
Jan 5, 2010 at 10:38
x = dotproduct(a[i], b[i], i)
.
Commented
Jan 7, 2010 at 22:12
In Python:
>>> x=5
>>> 1<x<10
True
>>> 1<x<3
False
Not a WTF, but a useful feature.
(10 > 5 > 1) != ((10 > 5) > 1)
in Python.
(funct_a(5)+5 > b > funct_a(5))
only calls funct_a(5)
once. It's a GREAT feature!
funct_a
will be called twice in that example. In b > funct_a(5) > c
it will only be called once though, as opposed to b > funct_a(5) and funct_a(5) > c
.
Commented
Feb 4, 2010 at 6:31
In Java:
int[] numbers() {
return null;
}
Can be written as:
int numbers() [] {
return null;
}
const T*
and T const*
are equivalent, it's T* const
that consts the pointer. Also, I hate sans fonts.
Commented
Jan 5, 2010 at 10:42
INTERCAL is probably the best compendium of strangest language features. My personal favourite is the COMEFROM statement which is (almost) the opposite of GOTO.
COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.
PLEASE
modifier often enough!
Not really a language feature, but an implementation flaw: Some early Fortran compilers implemented constants by using a constant pool. All parameters were passed by reference. If you called a function, e.g.
f(1)
The compiler would pass the address of the constant 1 in the constant pool to the function. If you assigned a value to the parameter in the function, you would change the value (in this case the value of 1) globally in the program. Caused some head scratching.
2+2
can equal 5
(for very large values of 2
of course!).
Commented
Jan 3, 2010 at 21:14
2+2
would equal 5
for small values of 5
).
Commented
Jan 4, 2010 at 22:17
2 + 2 = 5
; that'll be a syntax error. What will be true is 2 + 2 .EQ. 5
.
Commented
Jan 6, 2010 at 15:57
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 :)
std::vector<std::pair<int, std::complex>, std::allocator<std::pair<int, std::complex> > >::vector< std::vector<std::pair<int, std::complex>, std::allocator<std::pair<int, std::complex> > >::iterator>(std::vector<std::pair<int, std::complex>, std::allocator<std::pair<int, std::complex> > >::iterator, std::vector<std::pair<int, std::complex>, std::allocator<std::pair<int, std::complex> > >::iterator, std::allocator<std::pair<int, std::complex> >)
The many name spaces of C:
typedef int i;
void foo()
{
struct i {i i;} i;
i: i.i = 3;
printf( "%i\n", i.i);
}
Or with characters:
typedef char c;
void foo()
{
struct c {c c;} c;
c: c.c = 'c';
printf( "%c\n", c.c);
}
I would say the whole whitespace thing of Python is my greatest WTF feature. True, you more-or-less get used to it after a while and modern editors make it easy to deal with, but even after mostly full time python development for the past year I'm still convinced it was a Bad Idea. I've read all the reasoning behind it but honestly, it gets in the way of my productivity. Not by much, but it's still a burr under the saddle.
edit: judging by the comments, some people seem to think I don't like to indent my code. That is an incorrect assessment. I've always indented my code no matter what the language and whether I'm forced to or not. What I don't like is that it is the indentation that defines what block a line of code is in. I prefer explicit delimiters for that. Among other reasons, I find explicit delimiters makes it easier to cut and paste code.
For example, if I have a block indented 4 spaces and paste it at the end of a block that is indented 8 spaces, my editor (all editors?) have no idea if the pasted code belongs to the 8-space block or the outer block. OTOH, if I have explicit delimiters it's obvious which block the code belongs to and how it should be (re-)indented -- it does so by intelligently looking for block delimiters.
edit 2: some people who provide comments seem to think this is a feature I hate or that I think makes python a poor language. Again, not true. While I don't like it all that much, that's beside the point. The question is about the strangest language feature, and I think this is strange, by virtue of it being something very, very few (but >0) languages use.
I struggled a bit about this:
1;
In perl, modules need to return something true.
'Cogito ergo sum';
which as everyone knows is self-evidently true in all possible universes. This ensures maximum portability."
Commented
Jan 27, 2010 at 3:50
<?=1;?>
returns 1. <?=true;?>
returns 1. <?=false;?>
returns null.
Commented
Jun 11, 2010 at 4:39
I'm surprised that no one has mentioned Visual Basic's 7 loop constructs.
For i As Integer = 1 to 10 ... Next
While True ... End While
Do While True ... Loop
Do Until True ... Loop
Do ... Loop While True
Do ... Loop Until True
While True ... Wend
Because sticking an ! in front of your conditional is way too complicated!
While
and Whend
", since there are some people who do pronounce the word "while" with the voiceless labialised velar approximant. And of course it lines up nicer, and code that lines up is nice.
Wend
should have been a replacement for goto
. On Error Wend FixIt
I always wondered why the simplest program was:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Whereas it could be:
print "Hello World!"
Maybe this is to frighten computer science students in the first place ...
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 -lq
12^345
20774466823273785598434446955827049735727869127052322369317059031795\
19704325276892191015329301807037794598378537132233994613616420526484\
93077727371807711237016056649272805971389591721704273857856298577322\
13812114239610682963085721433938547031679267799296826048444696211521\
30457090778409728703018428147734622401526422774317612081074841839507\
864189781700150115308454681772032
bc
has been a standard Unix command for a long time.
Now for the "WTF feature". This is from man bc
(emphasis mine):
quit: When the quit statement is read, the bc processor is terminated, regardless of where the quit statement is found. For example, "if (0 == 1) quit" will cause bc to terminate.
halt: The halt statement (an extension) is an executed statement that causes the bc processor to quit only when it is executed. For example, "if (0 == 1) halt" will not cause bc to terminate because the halt is not executed.
JavaScript is object oriented, right? So running methods on literal strings and numbers should work. Like "hello".toUpperCase()
and 3.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:
3..toString()
The reason is that the literal 3.
is interpreted as 3.0
, and 3.0.toString()
works fine.
3..__add__(4)
). Then again I think (3).__add__(4)
is a much less brain damaged way to do it :)
3 .toString()
or (3).toString()
, too.
In JavaScript:
2 == [2]
// Even stranger
2 == [[[2]]]
// And down-right nutty
var a = { "abc" : 1 };
a[[[["abc"]]]] === a["abc"]; // this is also true
Luckily the kind folks at stackoverflow.com explained the whole thing to me: Why does 2 == [2] in JavaScript?
Number(n)
to do something similar. Unfortunately in both of our solutions ===
breaks =(.
My biggest most hated feature is any configuration file syntax which includes conditional logic. This sort of thing is rife in the Java world (Ant, Maven, etc. You know who you are!).
You just end up programming in a c**p language, with limited debugging and limited editor support.
If you need logic in your configuration the "Pythonic" approach of coding the configuration in a real language is much much better.
powerbasic (www.powerbasic.com) includes the compiler directive:
# BLOAT {bloatsize}
this increases the size of the compiled executable by <bloatsize>
bytes. this was put in the compiler in case people creating the executable don't like the small size of the generated executable. it makes the EXE seem bigger to compete with bloated programming languages:)