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

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


当前回答

在C:

int main() {
  int i = 0;
  int array[] = {1,2};

  return (i[array] + 1 == array[i]);
}

这个程序将返回1 (true)。

其他回答

在Python中,函数参数的“编译时”(或声明时)计算可能令人困惑:

def append(v, l = []):
    l.append(v)
    return l


print append(1)
print append(2)

>>> [1]
>>> [1,2]

其意图可能是:

def append(v, l = None):
    if l is None:
        l = []
    l.append(v)
    return l

print append(1)
print append(2)

>>> [1]
>>> [2]

这种行为对于缓存之类的事情很有用,但它可能是危险的。

附加特性:具有可变内容的元组:

a = (1,2,[3])
a[2][:] = [4] # OK
a[2] = [2] # crashes

这可能已经被提到了,但是——

PHP对八进制值的处理:

$a = 07; // 7 (as it should be)
$b = 08; // 0 (would be an error in any sensible language)
$c = 018; // 1 (again, should have been an error)
$d = 0A; // error (as it should be)

请看这里:http://bugs.php.net/bug.php?id=29676

还要注意关于这个错误的评论——Derick称其为一个特性(引用“fix”表示),而不是一个错误,他声称它会“在脚本中使用数字的所有情况下显著降低PHP的速度”——但是,为什么PHP会引发一个0A错误呢?

我认为人们可以写一整本书来讲述PHP的古怪之处……

在Haskell:

let 2 + 2 = 5 in 2 + 2

收益率5。

在c#中: A = cond ?B: c; 如果“b”和“c”是“赋值不兼容”,你永远不会得到结果,即使“a”是对象。 它是ms中最常用和最愚蠢的实现运算符。有关比较,请参阅D语言中的实现(关于类型推断的注意)。

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.