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

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


当前回答

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.

其他回答

引用尼尔·弗雷泽的话(看那一页的末尾)

try {
    return true;
} finally {
    return false;
}

(在Java中,但在JavaScript和Python中行为显然是相同的)。其结果留给读者作为练习。

编辑:只要我们谈到这个问题,也要考虑到这一点:

try {
    throw new AssertionError();
} finally {
    return false;
}

在JavaScript中,你可以使用双位负(~~n)来代替Math.floor(n)(如果n是正数)或parseInt(n, 10)(即使n是负数)。N | N和N & N总是得到和~~ N相同的结果。

var n = Math.PI;
n; // 3.141592653589793
Math.floor(n); // 3
parseInt(n, 10); // 3
~~n; // 3
n|n; // 3
n&n; // 3

// ~~n works as a replacement for parseInt() with negative numbers…
~~(-n); // -3
(-n)|(-n); // -3
(-n)&(-n); // -3
parseInt(-n, 10); // -3
// …although it doesn’t replace Math.floor() for negative numbers
Math.floor(-n); // -4

单个位的求反(~)计算-(parseInt(n, 10) + 1),因此两个位的求反将返回-(-(parseInt(n, 10) + 1) + 1)。

更新:这里有一个jsPerf测试用例,比较了这些替代方案的性能。

在Python中:

abs((10+5j)-(25+-5j))

返回~18.03,根据毕达哥拉斯定理,这是点(10,5)和(25,5)之间的距离。之所以会出现这种情况,是因为Python的原生语言支持2+2j形式的复数。由于复数的绝对值形式为a+bj =根号下(a^2+b^2),我们得到距离,同时从一个复数减去另一个复数,然后应用abs(绝对)函数在它上面。

让我们为所有试图废除保留词的语言(如PL/I)投票。

还有什么地方可以合法地写出这样有趣的表达:

IF IF THEN THEN = ELSE ELSE ELSE = THEN

(IF, THEN, ELSE是变量名)

or

IF IF THEN THEN ELSE ELSE

(IF为变量,THEN和ELSE为子程序)

在Common Lisp中,零维数组是很奇怪的,而且很自然地,它们具有读取语法。

? (aref #0A5)
5