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

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


当前回答

在Lisp中,你可以复制一个列表,可以复制一个向量,可以复制一个结构体,可以复制一个CLOS对象……

... 但是不能复制数组或哈希表。

其他回答

特点:Bash, Korn shell (ksh93)和Z shell都允许使用带或不带美元符号的变量下标数组:

array[index]=$value
array[$index]=$value

加上美元符号,会得到10000的期望值:

unset array
for i in {1..10000}
do
    ((array[$RANDOM%6+1]++))
done
unset total
for count in ${array[@]}
do
    ((total += count))
done
echo $total

陌陌性:如果你从RANDOM中移除美元符号,总数将随机变化,甚至大于10000。

类似地,这将产生3而不是2:

a=1; ((r[a++]++)); echo $a

你不能在这里用美元符号,因为这是赋值运算(a在lhs上)虽然你可以用间接的方法,但是双重求值还是会发生。

原因:对于美元符号,变量展开在算术求值之前执行,因此只执行一次。如果没有美元符号,它将执行两次,一次是计算查找的索引,另一次是计算赋值的索引(因此,实际上,循环中第一步的赋值可能看起来像array[4] = $array[6] + 1,这完全打乱了数组)。

在SQL Server中,如果你在生产代码中使用select *,你可能会遇到一个令人讨厌的意外。无论如何,使用select *并不被认为是一个好的实践,但了解一些有趣的行为是很好的。

参见问题“select * from table”vs“select colA,colB etc from table”在SqlServer2005中有趣的行为了解更多细节

INTERCAL可能是最奇怪的语言特征的最佳汇编。我个人最喜欢的是COMEFROM语句,它(几乎)与GOTO相反。

COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.

ActionScript 3:

当一个对象被它的接口使用时,编译器不识别从object继承的方法,因此:

IInterface interface = getInterface();
interface.toString();

给出一个编译错误。 解决方法是将类型转换为Object

Object(interface).toString();

PHP:

. 和+运算符。它有其合理的解释,但“a”+“5”= 5仍然显得尴尬。

Java(以及任何IEEE754的实现):

System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1);

输出0.9999999999999999

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

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

然而它可以是:

print "Hello World!"

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