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

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


当前回答

在PHP中:

echo 'foo' == 0;    // echos '1'
echo 'foo' == true; // echos '1'
echo 0 == true;     // echos '0'
$foo = 'foo';
echo $foo['bar']    // echos 'f'

PHP有一些最烦人的类型强制转换…

其他回答

这不是一个奇怪的功能,事实上,如果你仔细想想,它是完全有意义的,但还是给了我一个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
 }
};

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

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

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

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

在JavaScript(和Java)中,你可以转义这样有趣的字符:

var mystring = "hello \"world\"";

如果你想把回车放到字符串中,那是不可能的。你必须像这样使用\n:

var mystring = "hello, \nworld";

这是正常的,也是意料之中的——至少对于一种编程语言来说。奇怪的是你也可以像这样转义一个实际的回车:

var mystring = "hello, \
world";

C++:

void f(int bitand i){ //WTF
    i++;
}
int main(){
    int i = 0;
    f(i);
    cout << i << endl; //1
    return 0;
}

转变;

有时你会在perl方法的第一行中看到它来读取self指针