在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
一个意想不到的特性是,在C、c#、Ruby等语言中,枚举定义列表和数组初始化列表中的末尾逗号。
string[] foods = { "tofu", "grits", "cabbage", }
public enum ArtPeriod {
Modern,
Romantic,
Dada,
}
其他回答
Python的三元操作符
在Python中,C三元操作符(c++示例:bool isNegative = i < 0 ?True: false;)可用作语法糖:
>>> i = 1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is positive'
>>> i = -1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is negative!'
这并不奇怪,而是一种特征。奇怪的是,与C中的顺序(条件?答:b)。
在JavaScript(和Java)中,你可以转义这样有趣的字符:
var mystring = "hello \"world\"";
如果你想把回车放到字符串中,那是不可能的。你必须像这样使用\n:
var mystring = "hello, \nworld";
这是正常的,也是意料之中的——至少对于一种编程语言来说。奇怪的是你也可以像这样转义一个实际的回车:
var mystring = "hello, \
world";
Perl。
输出“Foo\n”除非$ Foo;
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.
更多的是平台特性而不是语言特性:在iPhone上,创建一个无限循环,里面有一些计算,然后运行你的程序。你的手机会发热,当外面冷的时候,你可以把它当手暖。