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

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


当前回答

在Ruby中…

i=true
while(i)
   i=false
   a=2
end
puts defined?(a) // returns true

其他回答

这不是一个奇怪的功能,事实上,如果你仔细想想,它是完全有意义的,但还是给了我一个WTF时刻。

在c++(和c#)中,基类的子类不能访问基实例上的私有和受保护成员。

class Base {
protected:
 m_fooBar;
};

class Derived: public Base {
public:
 void Test(Base& baseInstance) {
  m_fooBar=1; //OK
  baseInstance.m_fooBar = 1; //Badness
  //This, however is OK:
  ((Derived&)baseInstance).m_fooBar = 1; //OK
 }
};

Python的三元操作符

在Python中,C三元操作符(c++示例:bool isNegative = i < 0 ?True: false;)可用作语法糖:

>>> i = 1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is positive'
>>> i = -1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is negative!'

这并不奇怪,而是一种特征。奇怪的是,与C中的顺序(条件?答:b)。

这并不是一个奇怪的特性,但从类型安全的角度来看,它确实令人恼火:c#中的数组协方差。

class Foo { }
class Bar : Foo { }
class Baz : Foo { }

Foo[] foo = new Bar[1];
foo[0] = new Baz(); // Oh snap!

我相信这是从Java继承而来的(双关语)。

JavaScript八进制转换“特性”是一个很好的了解:

parseInt('06') // 6
parseInt('07') // 7
parseInt('08') // 0
parseInt('09') // 0
parseInt('10') // 10

详情请点击这里。

Perl充满了奇怪但整洁的特性。

If可以用在语句之前或之后,就像这样:

print "Hello World" if $a > 1;    
if ($a > 1) { print "Hello World"; }

foreach也是如此:

print "Hello $_!\n" foreach qw(world Dolly nurse);