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

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


当前回答

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

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

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

其他回答

Perl中的字符串数学非常奇怪。

$ perl -E '$string = "a"; $string++; say $string'
b

$ perl -E '$string = "abc"; $string++; say $string'
abd

$ perl -E '$string = "money"; $string++; say $string'
monez

$ perl -E '$string = "money"; $string--; say $string'
-1

Perl文件句柄样式的操作符调用。

一开始是有的

print "foo", "bar", "baz"; # to stdout
print STDERR "foo", "bar", "baz";

注意没有逗号,这样你就知道这是要打印到的文件句柄,而不是要以字符串化方式打印的文件句柄。这是一个肮脏的黑客。

语言升级开始了,他们制作了适当的OO文件句柄,并将x FOO y, z, abc转换为FOO->x(y, z, abc)。有点可爱。相同的print语句有效地运行

STDERR->print("foo", "bar", "baz");

大多数情况下,当你错过一个逗号时,或者尝试运行hashof $a, $b, $c(不带括号的子例程调用)之类的东西时,你会注意到这一点,而忘记从它的实用程序包中将hashof函数导入到你的命名空间中,你会得到一个奇怪的错误消息,关于“无法通过字符串$a的包内容调用方法'hashof'”。

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

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

在Scala中,没有操作符,只有方法。所以a + b - c实际上等于a +(b) -(c)在这里,它等于Smalltalk。但是,与Smalltalk不同的是,它考虑了优先级。规则基于第一个字符,因此一个假设的方法*+将优先于一个叫做+*的方法。做了一个例外,以便任何以=结尾的方法都将具有与==——含义相同的优先级!!和!=(非假设方法)具有不同的优先级。

所有ASCII字母的优先级最低,但所有非ASCII (unicode)字符的优先级最高。如果你写了一个比较两个int型的方法,那么2 + 2 = 1 + 3将会编译并为真。如果你用葡萄牙语写é,那么2 + 2 é 1 + 3会导致错误,因为它会看到2 + (2 é 1) + 3。

而且,在Scala中操作符的WTF中,所有以:结尾的方法都是右关联的,而不是左关联的。这意味着1::2::Nil等价于Nil.::(2).::(1)而不是1.::(2).::(Nil)。

PHP对字符串中数值的处理。详见之前对另一个问题的回答,但简而言之:

"01a4" != "001a4"

如果你有两个包含不同数量字符的字符串,它们不能被认为是相等的。前导零很重要,因为它们是字符串而不是数字。

"01e4" == "001e4"

PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates "01e4" == "10000" as true because these are numbers with equivalent values. This is documented behaviour, it’s just not very sensible.