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

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


当前回答

在c#中,你可以在接口上使用new操作符。

其他回答

很简单,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!

在PHP中,你可以使用符号和字符串文字或包含变量名的变量引用变量,例如:

${'foo'} = 'test';
echo $foo;

这将打印“test”。这种行为的奇怪之处在于,你也可以使用非字符串作为变量名,例如:

${array()} = 'test';
echo ${array()};
${NULL} = 'test';
echo ${NULL};

现在我们有了名为array()的变量,甚至还有NULL!所有包含字符串"test"。

Python的everything-is-really-a-reference有一个有趣的副作用:

>>> a = [[1]] * 7
>>> a
[[1], [1], [1], [1], [1], [1], [1]]
>>> a[0][0] = 2
>>> a
[[2], [2], [2], [2], [2], [2], [2]]

在我看来,这在c++中是不允许的:

class A {
public:
  virtual string foo(){return "A::foo";}
};

class B : public A {
public:
  virtual string foo(){return "B::foo";}
};

int main () {
  B* b = new B();
  // In my opinion the following should not be allowed
  cout << b->A::foo() << endl;  // Will print "A::foo"
}

这似乎是正确的,但这意味着如果不允许子类的用户调用原始方法而不是新方法,就不能重写方法。

只要考虑一个集合的子类,当向集合本身添加一个元素时,您希望增加元素的数量。

一个合乎逻辑的解决方案是重写add()方法,在添加元素之前增加计数器,但是新集合的用户可以使用旧方法向其添加元素,这样就绕过了增量,导致元素计数器与集合的实际元素数量不一致。

这在Java中是不可能的。

Perl。

输出“Foo\n”除非$ Foo;