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

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


当前回答

交替:在许多语言中的事物之间交替:

boolean b = true;
for(int i = 0; i < 10; i++)
  if(b = !b)
    print i;

乍一看,b怎么可能不等于它自己呢? 这实际上只会打印奇数

其他回答

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)。

我一直在想为什么最简单的程序是:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

然而它可以是:

print "Hello World!"

也许这是为了吓唬计算机科学专业的学生……

Python for循环中的else。

来自Python文档:

for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print n, 'equals', x, '*', n/x
            break
    else:
        # loop fell through without finding a factor
        print n, 'is a prime number'

输出:

2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

这里有一大堆奇怪的C特性:http://www.steike.com/code/useless/evil-c/

在x++ (Microsoft Dynamics AX):

1)需要在单独的行上使用分号(;)将变量声明与语句分开(至少在4.0版本之前)

    int i;
    int myArray[5];
    ;
    i = 1;

2)数组索引是基于1的,所以你不允许像in那样使用索引0(零)从数组中读取

    int myArray[5];
    ;
    print myArray[0];    // runtime error

这并不奇怪,但是你可以在赋值的左边使用0索引,比如in

    int myArray[5];
    ;
    myArray[2] = 102;
    myArray[0] = 100;    // this is strange
    print myArray[2];    // expcting 102?

会发生什么呢?数组被初始化为它的默认值,无论赋值中使用了什么值。上面的代码输出0(零)!