在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Perl的$[(已弃用),这在之前关于通用Perl变量的另一篇文章中提到过,但它值得特别提到,并有更好的解释。对$[的更改仅限于当前范围。更多的信息以及如何使用它的含义;)可以在$[中找到,请访问http://www.perlmonks.org/index.pl/?node_id=480333
其他回答
我一直在想为什么最简单的程序是:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
然而它可以是:
print "Hello World!"
也许这是为了吓唬计算机科学专业的学生……
对于那些不知道的人,bc是一种“任意精度计算器语言”,我经常使用它进行快速计算,特别是当涉及的数字很大的时候($是提示符):
$ bc -lq
12^345
20774466823273785598434446955827049735727869127052322369317059031795\
19704325276892191015329301807037794598378537132233994613616420526484\
93077727371807711237016056649272805971389591721704273857856298577322\
13812114239610682963085721433938547031679267799296826048444696211521\
30457090778409728703018428147734622401526422774317612081074841839507\
864189781700150115308454681772032
bc在很长一段时间内都是标准的Unix命令。
现在是“WTF功能”。这是来自man bc(强调是我的):
quit:当读取quit语句时,bc处理器被终止, 不管在哪里找到quit语句。 例如,"if (0 == 1) quit"将导致bc终止。 halt: halt语句(扩展)是一个执行的语句 导致BC处理器仅在执行时退出。 例如,"if (0 == 1) halt"不会导致bc终止,因为停止没有执行。
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.
在c++
const char* wtf()
{
char buf[100];
return buf;
}
string s = ... + wtf() + ...;
在s中创建有趣的值。部分是字符串,部分是堆栈内容,与零混合,因此s.length()!=strlen(s.c_str())。 最奇怪的是,编译器在返回指向堆栈的指针时完全没有问题——如果他在那里添加了一个警告,编译器程序员的手可能会掉下来。
Perl的sub没有真正的参数列表,只有@_数组。同时,sub会自动变平传入的参数。
我不明白为什么这是一个持久的功能;这反映了几年前我不得不在TI-86 BASIC上做的事情,因为这门语言没有足够的特色。