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

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


当前回答

C# has a feature called "extension methods", which are roughly analogous to Ruby mix-ins - Essentially, you can add a method to any pre-existing class definition (for instance, you oould add "reverse()" to String if you were so inclined). That alone is fine- The "Weird" part is that you can add these extension methods, with a method body and everything, to an interface. On the one hand, this can be handy as a way to add a single method to a whole swath of classes which aren't part of the same inheritance tree. On the other, you're adding fleshed out methods to interfaces, essentially breaking the very definition of an interface.

其他回答

我有点纠结:

1;

在perl中,模块需要返回true。

COMEFROM是我见过的最奇怪,也可能是最没用的语言功能。

其次是三元运算符,因为它违反了优化的第一条规则。它带来的危害大于它解决的问题。它的危害更大,因为它使代码可读性更差。

并不是一个真正的语言功能,但有趣/很棒的功能使用是Duff的设备。

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.

在JavaScript中,方法的结果取决于所放置的样式花括号。这是K&R风格,其中括号放在方法签名之后和return语句之后:

var foo = function() {
  return {
    key: 'value'
  };
}

foo() // returns an object here

现在,如果我将这段代码格式化为Allman风格,其中括号总是放在新行上,结果是不同的:

var foo = function()
{
  return
  {
    key: 'value'
  };
}

foo() // returns undefined here

如何来吗?在JavaScript中,如果您不自己做,语言会自动在每行末尾放置分号。所以在最后一个代码片段中真正发生的是这样的:

var foo = function()
{
  return; // here's actually a semicolon, automatically set by JavaScript!
  {
    key: 'value'
  };
}

因此,如果调用foo(),方法中的第一个语句将是一个返回语句,该语句将返回undefined,并且不会执行后面的其他语句。

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

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

IF IF THEN THEN = ELSE ELSE ELSE = THEN

(IF, THEN, ELSE是变量名)

or

IF IF THEN THEN ELSE ELSE

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