在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在PHP中
var_export('false' == 0); // true
var_export('false' == true); // true
var_export('false' == false); // false
EDIT
正如@Kobi提到的,这可能发生,因为语言将任何值解释为“TRUE”,除了“FALSE”,但在PHP中不是这样,在那里事情比你想象的更奇怪!
这个案例在PHP手册的“字符串转换到数字”一章中有完整的记录,它说:
如果字符串以valid开头 数值数据,这就是值 使用。否则,该值为0 (零)。
这里有一个例子:
print (int) 'zero'; // 0
print (int) 'false'; // 0
// but
print (int) '1 - one'; // 1
附注:我认为这种隐式类型转换弊大于利。
其他回答
Ruby
时间。Parse经常假装解析没有失败,而是返回现在
require 'time'
Time.parse '2000-01-01 12:00:00'
# -> 2000-01-01 12:00:00 +0100
Time.parse '2000-99-01 00:00:00'
# -> ArgumentError: argument out of range ...
Time.parse 'now'
# -> 2010-08-13 21:26:13 +0200
Time.parse 'yesterday'
# -> 2010-08-13 21:26:18 +0200
Time.parse 'billion years ago'
# -> 2010-08-13 21:26:37 +0200
Common Lisp的format函数有一个选项,可以将数字打印为罗马数字。
在INTERCAL中,这是你能得到的唯一输出形式。
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.
In C
a[i++] = i;
它会编译,但很少执行您认为它应该执行的操作。优化更改会产生截然不同的结果。它在不同平台上的运行方式也不同。
然而,编译器对此非常满意。
在JavaScript中:
'5' + 3 gives '53'
而
'5' - 3 gives 2