在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

在Java中:

int[] numbers() {
  return null;
}

可以写成:

int numbers() [] {
  return null;
}

其他回答

PHP对字符串中数值的处理。详见之前对另一个问题的回答,但简而言之:

"01a4" != "001a4"

如果你有两个包含不同数量字符的字符串,它们不能被认为是相等的。前导零很重要,因为它们是字符串而不是数字。

"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.

在c#中,以下代码会生成编译器错误“无法从方法组转换为委托”。虽然背后的逻辑是合理的,但我还是觉得很奇怪。

control.BeginInvoke(delegate { DoSomething(); });

Perl充满了奇怪但整洁的特性。

If可以用在语句之前或之后,就像这样:

print "Hello World" if $a > 1;    
if ($a > 1) { print "Hello World"; }

foreach也是如此:

print "Hello $_!\n" foreach qw(world Dolly nurse);

到目前为止,我遇到过的最奇怪的功能是BASIC方言中的“RETURN n”语句(不记得是哪一种了,这是大约28年前的事了)。“n”是可选的,默认为1。它可以是一个正数或负数,指示下一个执行的是与调用GOSUB相关的哪一行。

例如,下面将输出“30”:

10 GOSUB 200
20 PRINT "20"
30 PRINT "30"
100 END
200 RETURN +2

当我必须把一个用这种奇怪的BASIC语言编写的程序翻译成FORTRAN时,我遇到了这个问题。BASIC程序相当多地使用了这个特性,根据不同的条件返回不同的语句,我花了一段时间来理解逻辑流。一旦我理解了它,我就可以编写一个更简单的程序版本。不用说,更简单的FORTRAN版本比原来的BASIC程序bug更少。

BrainFuck语言。真的是这样!