在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Javascript中,我认为以下是等价的:
a['title'] = "Syntactic sugar is good for yr teeth.";
a.title = "Syntactic sugar is good for yr teeth.";
其他回答
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'”。
很简单,Erlang有很多。例如,标点符号的三种形式,
a_function(SomeVariable) ->
statements_end_with_commas(),
case PatternMatching of
0 -> now_we_end_with_semicolon;
true -> except_the_last_one
end.
%% Function definitions end with periods!
一些早期的动态语言(包括,如果我没记错的话,Perl的早期版本)没有弄清楚什么是好的动态,什么是坏的动态。所以他们中的一些人允许这样做:
1 = 2;
在这句话之后,下列情况是正确的:
if(1 + 1 == 4)
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,这完全打乱了数组)。