在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“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.

其他回答

这个古老的PHP宠儿本身并不完全是WTFish,但作用域解析错误是许多开发人员看到的事情之一,值得给一些WTF的爱:

$class = new StdClass();
$class::test();

PHP Parse error:  syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM on line 3

这缺少了一个奇怪的特性:Python没有switch语句(尽管存在变通方法)。

很简单,Erlang有很多。例如,标点符号的三种形式,

a_function(SomeVariable) ->
  statements_end_with_commas(),
  case PatternMatching of
    0 -> now_we_end_with_semicolon;
    true -> except_the_last_one
  end.

%%  Function definitions end with periods!

在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

X = X + 1

当我还是初学者的时候,这很难消化,而现在函数式语言不使用它了,这就更难了!

如果你不明白这有多奇怪:把等号看作断言语句,而不是赋值操作,就像你在基础代数中经常做的那样,那么这相当于说“0 = 1”。