在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Perl
my %h1 = map { $_ => 1 } qw/foo bar baz/; // construct an 'is_member' type lookup table
my %h2 = map { "$_" => 1 } qw/foo bar baz/;
第二行是一个语法错误,即使对一个有经验的perl程序员来说,它看起来是一样的。perl的缺点是总是试图做你想做的,而不是你说的。
其他回答
在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)。
Java有一整本关于它们的书。
书http://www.javapuzzlers.com/lg-puzzlers-cropped.jpg
爪哇益智游戏
我曾经写过一种编程语言,它有一个“strfry”操作符:
"hello world"?
# => "wdo rlholle"
有用的,是吗?
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
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.