在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Java的Integer类的基转换静态方法。P似乎很少有语言内置了这个功能。
其他回答
COMEFROM是我见过的最奇怪,也可能是最没用的语言功能。
其次是三元运算符,因为它违反了优化的第一条规则。它带来的危害大于它解决的问题。它的危害更大,因为它使代码可读性更差。
并不是一个真正的语言功能,但有趣/很棒的功能使用是Duff的设备。
在Java中,
Int x = 010;
这将x赋值为8。
在Java中,任何前面带0的整数都被假定为八进制。
In C
a[i++] = i;
它会编译,但很少执行您认为它应该执行的操作。优化更改会产生截然不同的结果。它在不同平台上的运行方式也不同。
然而,编译器对此非常满意。
Haskell's use of Maybe and Just. Maybe a is a type constructor that returns a type of Just a, but Maybe Int won't accept just an Int, it requires it to be a Just Int or Nothing. So in essence in haskell parlance Just Int is about as much of an Int as an apple is an orange. The only connection is that Just 5 returns a type of Maybe Interger, which can be constructed with the function Just and an Integer argument. This makes sense but is about as hard to explain as it can theoretically be, which is the purpose of haskell right? So is Just really JustKindaLikeButNotAtAll yea sorta, and is Maybe really a KindaLooksLikeOrIsNothing, yea sorta again.
-- Create a function that returns a Maybe Int, and return a 5, which know is definitly Int'able
> let x :: Maybe Int; x = 5;
<interactive>:1:24:
No instance for (Num (Maybe Int))
arising from the literal `5' at <interactive>:1:24
Possible fix: add an instance declaration for (Num (Maybe Int))
In the expression: 5
In the definition of `x': x = 5
> Just 5
Just 5
it :: Maybe Integer
-- Create a function x which takes an Int
> let x :: Int -> Int; x _ = 0;
x :: Int -> Int
-- Try to give it a Just Int
> x $ Just 5
<interactive>:1:4:
Couldn't match expected type `Int' against inferred type `Maybe t'
In the second argument of `($)', namely `Just 5'
In the expression: x $ Just 5
In the definition of `it': it = x $ Just 5
祝你好运读到这篇文章,我希望它是正确的。
我喜欢Smalltalk中缺少运算符优先级
2 * 3 + 4 * 5 = 6 + 4 * 5 = 10 * 5 = 50
而不是
2 * 3 + 4 * 5 = 6 + 4 * 5 = 6 + 20 = 26
这是由于smalltalk的对象性质和消息从左向右传递的事实。如果消息*以数字3作为参数发送给2,则该消息的响应为6。太棒了,如果你觉得邪恶,你甚至可以用猴子来修补它。