在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“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

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

其他回答

Perl文件句柄样式的操作符调用。

一开始是有的

print "foo", "bar", "baz"; # to stdout
print STDERR "foo", "bar", "baz";

注意没有逗号,这样你就知道这是要打印到的文件句柄,而不是要以字符串化方式打印的文件句柄。这是一个肮脏的黑客。

语言升级开始了,他们制作了适当的OO文件句柄,并将x FOO y, z, abc转换为FOO->x(y, z, abc)。有点可爱。相同的print语句有效地运行

STDERR->print("foo", "bar", "baz");

大多数情况下,当你错过一个逗号时,或者尝试运行hashof $a, $b, $c(不带括号的子例程调用)之类的东西时,你会注意到这一点,而忘记从它的实用程序包中将hashof函数导入到你的命名空间中,你会得到一个奇怪的错误消息,关于“无法通过字符串$a的包内容调用方法'hashof'”。

交替:在许多语言中的事物之间交替:

boolean b = true;
for(int i = 0; i < 10; i++)
  if(b = !b)
    print i;

乍一看,b怎么可能不等于它自己呢? 这实际上只会打印奇数

在PHP中,你可以使用符号和字符串文字或包含变量名的变量引用变量,例如:

${'foo'} = 'test';
echo $foo;

这将打印“test”。这种行为的奇怪之处在于,你也可以使用非字符串作为变量名,例如:

${array()} = 'test';
echo ${array()};
${NULL} = 'test';
echo ${NULL};

现在我们有了名为array()的变量,甚至还有NULL!所有包含字符串"test"。

在C或c++中,sizeof…参数的圆括号是可选的。假设参数不是类型:

void foo() {
  int int_inst;

  // usual style - brackets ...
  size_t a = sizeof(int);
  size_t b = sizeof(int_inst);
  size_t c = sizeof(99);

  // but ...
  size_t d = sizeof int_inst; // this is ok
  // size_t e = sizeof int; // this is NOT ok
  size_t f = sizeof 99; // this is also ok
}

我一直不明白这是为什么!

在x++ (Microsoft Dynamics AX):

1)需要在单独的行上使用分号(;)将变量声明与语句分开(至少在4.0版本之前)

    int i;
    int myArray[5];
    ;
    i = 1;

2)数组索引是基于1的,所以你不允许像in那样使用索引0(零)从数组中读取

    int myArray[5];
    ;
    print myArray[0];    // runtime error

这并不奇怪,但是你可以在赋值的左边使用0索引,比如in

    int myArray[5];
    ;
    myArray[2] = 102;
    myArray[0] = 100;    // this is strange
    print myArray[2];    // expcting 102?

会发生什么呢?数组被初始化为它的默认值,无论赋值中使用了什么值。上面的代码输出0(零)!