在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Ruby中,你可以用heredocs做一些奇怪的事情。考虑:
a = <<ONE
This is one. #{<<TWO}
This is two. #{<<THREE}
This is three.
THREE
TWO
ONE
p a # => "This is one. This is two. This is three.\n\n\n"
其他回答
在Scala中,没有操作符,只有方法。所以a + b - c实际上等于a +(b) -(c)在这里,它等于Smalltalk。但是,与Smalltalk不同的是,它考虑了优先级。规则基于第一个字符,因此一个假设的方法*+将优先于一个叫做+*的方法。做了一个例外,以便任何以=结尾的方法都将具有与==——含义相同的优先级!!和!=(非假设方法)具有不同的优先级。
所有ASCII字母的优先级最低,但所有非ASCII (unicode)字符的优先级最高。如果你写了一个比较两个int型的方法,那么2 + 2 = 1 + 3将会编译并为真。如果你用葡萄牙语写é,那么2 + 2 é 1 + 3会导致错误,因为它会看到2 + (2 é 1) + 3。
而且,在Scala中操作符的WTF中,所有以:结尾的方法都是右关联的,而不是左关联的。这意味着1::2::Nil等价于Nil.::(2).::(1)而不是1.::(2).::(Nil)。
闲聊:
在类Test中有一个类方法,返回一个常量字符串:
method1
^ 'niko'
无论发生什么,这个方法都会不断返回字符串'niko'。但事实并非如此。
s := Test method1
(设置为“niko”。)
s at: 4 put: $i.
(设置为“niki”。)
s := Test method1
(再次设置为“niki”。)
因此,第二行代码永久地将method1更改为返回'niki'而不是'niko',即使方法的源代码没有更新。
Java有一整本关于它们的书。
书http://www.javapuzzlers.com/lg-puzzlers-cropped.jpg
爪哇益智游戏
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
祝你好运读到这篇文章,我希望它是正确的。
Duff的C语言设备!
在C语言中,可以将do/while语句与switch语句交错使用。下面是memcpy使用此方法的示例:
void duff_memcpy( char* to, char* from, size_t count ) {
size_t n = (count+7)/8;
switch( count%8 ) {
case 0: do{ *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
}while(--n>0);
}
}