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

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


当前回答

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

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

其他回答

c# yield语句,并不奇怪,但非常有用。

http://msdn.microsoft.com/en-us/library/9k7k7cf0 (VS.80) . aspx

这让我惊艳

#define _ -F<00||--F-OO--;
int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()
{
            _-_-_-_
       _-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
       _-_-_-_-_-_-_-_
           _-_-_-_
}

在JavaScript中:

2 == [2]

//更陌生 2 == [[[2]]]

//和彻头彻尾的坚果 Var a = {"abc": 1}; A [[[["abc"]]]] === A ["abc"];//这也是正确的

幸运的是,stackoverflow.com网站上善良的人们向我解释了这一切:http:/stackoverflow.com/questions/1724255/why-does-2-2-in-javascript

可能已经说过了(也许这对一些人来说并不奇怪),但我认为这很酷:

在Javascript中,声明函数接受的参数只是为了方便程序员。通过函数调用传递的所有变量都可以通过关键字“arguments”访问。所以下面会提醒“world”:

<script type="text/javascript">

function blah(){
alert(arguments[1]);
}

blah("hello", "world");

</script> 

注意,虽然这些参数看起来像是存储在数组中(因为您可以以与数组元素相同的方式访问对象属性),但事实并非如此。arguments是一个对象,而不是数组(因此,它们是存储在数值索引下的对象属性),如下例所示(typeOf函数来自Crockford的JavaScript补救页面):

argumentsExample = function(){
    console.log(typeOf(arguments));

    anArray = [];
    console.log(typeOf(anArray));

    anObject = {};
    console.log(typeOf(anObject));
}

function typeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (typeof value.length === 'number' &&
                    !(value.propertyIsEnumerable('length')) &&
                    typeof value.splice === 'function') {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}

argumentsExample("a", "b");

在统一中,

GameObject.Find("MyObject")

将正常返回对象。然而,如果你这样做:

GameObject.Find("MyObject").active = false;
//do other stuff
if (GameObject.Find("MyObject").active)
{
    //do stuff
}

然后您将得到一个空引用。在Unity iPhone中,这段代码通常在编辑器中正常工作,但在iPhone上运行时会导致SIGBUS。问题是GameObject.Find()将只定位活动对象,所以即使你只是检查它是否活动,你也会有效地调用if (null.active)。

为了使它正常工作,你必须在使它不活跃之前存储它。

GameObject obj = GameObject.Find("MyObject");
obj.active = false;
//do other stuff
if (obj.active)
{
    //do stuff
}

可以说,这是一种更好的实践,但Unity处理非活动对象的方式通常是相当奇怪的。它似乎卸载了大部分非活动对象(纹理等),但不是全部,因此非活动对象仍然会消耗大量内存。