在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
从Ruby中的随机类继承:
class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample
...
end
(首次出现在Ruby的隐藏特性中)
其他回答
Haskell又说:
在Haskell中,你可以处理任意大小的文件,就好像它是一个简单的字符串。只有在实际使用字符串时,文件才会被读取。由于Haskell令人难以置信的懒惰,这样的程序将在恒定的空间中运行,而不管文件的大小:
main = interact (>>= \x -> if x == '\n' then "\r\n" else [x])
(这个程序将一个文件从stdin转换为stdout,并将LF替换为CRLF,交互函数将整个stdin输入到一个函数,并将输出移动到stdout。)
这种惰性也可能导致问题,因为如果关闭一个文件句柄,就不能完全shure,不管Haskell是否已经解析了其中的所有数据。
在ruby/python/c中,你可以像这样连接字符串:
a = "foo" "bar"
print a # => "foobar"
INTERCAL中的一元运算符(AND, OR和XOR)。
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和c++中的三联体。
int main() {
printf("LOL??!");
}
这将打印LOL|,因为trigraph ??!转换为|。