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

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


当前回答

Java泛型 都是WTF:

List<String> ls = new ArrayList<String>(); //1
List<Object> lo = ls; //2

2:是非法的(??)这是令人费解的,但你必须想想接下来会发生什么:

lo.add(new Object());
String s = ls.get(0);

我们将对象赋值给字符串引用,哦不!就像这样,他们周围有很多陷阱。

其他回答

A very tiny thing that annoyed me in COBOL was that there was no dedicated modulo operation. Instead you could do a division specifying that you only wanted whole number results and store the rest in a different variable. Since COBOL is very sensitive when it comes to variables that means that you ended up with a variable you didn't really need, i.e. the actual result of the division. This is the story of how I once named a variable "USELESS" - that was the most appropriate name I could think of.

我几年前使用的一个Fortran编译器有一个有趣的特性:(A)数字先存储在高字节;(b)数字按地址传递给子程序;(c)没有在编译时检查长度。

所以你可以写一个这样的程序:(如果我把语法弄乱了,请原谅。我已经很久没有写Fortran了。)

INTEGER*2 FUNCTION TIMESTWO (INTEGER*2 N)
RETURN N*2

... THEN CALL THIS SOMEWHERE WITH A LONG INTEGER ...

INTEGER*4 I, J

I=42
J=TIMESTWO(I)

J的最终值是…零!

为什么?因为传入的值是4个字节,但被调用的函数只查看前两个字节。由于前两个是0,它将0乘以2并返回它。然后将此返回值转换回四个字节。

当我第一次遇到它时,它非常神秘。几乎我传递给某些函数的每个数字都被解释为零!

这并不是说它被大量使用,而是c++的“返回对静态大小数组的引用”的语法很奇怪:

struct SuperFoo {
  int (&getFoo() const)[10] {
    static int foo[10];
    return foo;
  }
}

在上述情况下,Ofc方法可以声明为静态const

我一直是PHP错误的忠实粉丝,当在一行中使用两个冒号时脱离上下文:

解析错误:语法错误,第3行/path/to/file/error.php中的T_PAAMAYIM_NEKUDOTAYIM异常

第一次遇到这种情况时,我完全被弄糊涂了。

C++:

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