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

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


当前回答

JavaScript八进制转换“特性”是一个很好的了解:

parseInt('06') // 6
parseInt('07') // 7
parseInt('08') // 0
parseInt('09') // 0
parseInt('10') // 10

详情请点击这里。

其他回答

在Perl中,你可以做到:

my $test = "Hello World";
substr($test, 0, 5) = "Goodbye";

print $test;

这在其他语言中可行吗?

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.

一个意想不到的特性是,在C、c#、Ruby等语言中,枚举定义列表和数组初始化列表中的末尾逗号。

string[] foods = { "tofu", "grits", "cabbage", }

public enum ArtPeriod {
  Modern,
  Romantic,
  Dada,
}

这可能已经被提到了,但是——

PHP对八进制值的处理:

$a = 07; // 7 (as it should be)
$b = 08; // 0 (would be an error in any sensible language)
$c = 018; // 1 (again, should have been an error)
$d = 0A; // error (as it should be)

请看这里:http://bugs.php.net/bug.php?id=29676

还要注意关于这个错误的评论——Derick称其为一个特性(引用“fix”表示),而不是一个错误,他声称它会“在脚本中使用数字的所有情况下显著降低PHP的速度”——但是,为什么PHP会引发一个0A错误呢?

我认为人们可以写一整本书来讲述PHP的古怪之处……

在Matlab中,以下内容可能会让你感到惊讶,特别是如果你习惯了Python:

>> not true

ans =

     0     0     0     0
>> not false

ans =

     0     0     0     0     0

这里有两个奇怪的特征。第一个是a b被解释为a('b'),因此非真被解释为not('true')。第二个奇怪的特征是没有任何字符返回0(大概是因为在matlab中没有false或true,只有0或1)。