在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
我喜欢在C中插入八进制值:
int values[8] = { 123, 154, 103, 310, 046, 806, 002, 970 };
其他回答
在ColdFusion中,文本值自动转换为各种数据类型,用于各种目的。我遇到了一个奇怪的问题,“00A”和“000”被返回为相等。事实证明,ColdFusion将“00A”解释为时间,转换为某种数字时间格式,并将其转换为0。“000”被转换为0。所以它们都被认为是相等的。那时我学习了字符串的比较函数。
C + + 1 xλ的:
[] (int x) { std::cout << x << std::endl; } ();
它们可能被滥用在一些奇怪的语法中:
[](){}();[]{[]{}();}();
这是完全有效的c++ 1x。
这可能已经被提到了,但是——
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的古怪之处……
A very tiny thing that annoyed me in COBOL was that there was no dedicated modulo operation. Instead you could do a division specifying that you only wanted whole number results and store the rest in a different variable. Since COBOL is very sensitive when it comes to variables that means that you ended up with a variable you didn't really need, i.e. the actual result of the division. This is the story of how I once named a variable "USELESS" - that was the most appropriate name I could think of.
特点: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,这完全打乱了数组)。