在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

在类C语言(包括C本身)中,你可以使用“向下到”操作符:

for (x = 20; x --> 0;) {
    print x;
}

这将打印从19到0的数字。

其他回答

在PHP中

var_export('false' == 0);       // true

var_export('false' == true);    // true

var_export('false' == false);   // false

EDIT

正如@Kobi提到的,这可能发生,因为语言将任何值解释为“TRUE”,除了“FALSE”,但在PHP中不是这样,在那里事情比你想象的更奇怪!

这个案例在PHP手册的“字符串转换到数字”一章中有完整的记录,它说:

如果字符串以valid开头 数值数据,这就是值 使用。否则,该值为0 (零)。

这里有一个例子:

print (int) 'zero';    // 0
print (int) 'false';   // 0
// but
print (int) '1 - one'; // 1

附注:我认为这种隐式类型转换弊大于利。

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

祝你好运读到这篇文章,我希望它是正确的。

我为客户端编写了一种编程语言(用于实验驱动定制硬件),其中包含一些定制类型(Curl, Circuit,…),每种类型只有2个值。它们可以隐式地转换为布尔值,但是(在客户机的请求下)可以在运行时更改此类类型常量的确切布尔值。

例如: Curl类型有2个可能的值:CW和CCW(顺时针和逆时针)。在运行时,你可以通过一个简单的赋值语句改变布尔值:

ccw := true

所以你可以改变所有这些类型值的布尔值。

作为一名NHibernate爱好者,当我从Smalltalk听到be时,我非常激动……如。

a become: b

它直接将a对象更改为b,这使得编写惰性初始化代理变得很简单,因为所有对a的引用现在都将引用b。非常简洁!

我认为这是一种奇怪的语言特征,因为据我所知,没有其他语言具有这种能力。

在ruby/python/c中,你可以像这样连接字符串:

a = "foo" "bar"
print a # => "foobar"