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

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


当前回答

这里有一大堆奇怪的C特性:http://www.steike.com/code/useless/evil-c/

其他回答

在Java中从文本文件中读取一行。

BufferedReader in = null;
try {
   in = new BufferedReader(new FileReader("filename"));
   String str;
   str = in.readLine();
   if (str != null) {
      ...
   } 
} catch (IOException e) {
   ...
} finally {
   try {
      if (in != null) {
         in.close();
      }
   } catch (IOException e) {}
}

啊。虽然我承认这并不奇怪……只是邪恶。: -)

更短、更习惯的版本:

try {
   BufferedReader in = new BufferedReader(new FileReader("filename"));
   try {
       String str = in.readLine();
       while (str != null) {
          str = in.readLine();
       } 
    } finally {
        in.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}

在J中,大多数原语(也就是函数)是一元的(一个参数)或二元的(两个参数,一个在左边,一个在右边)。但是修正原语需要3个(我认为这是唯一一个,除了foreign)。这是可以理解的,它需要3个,但它只是看起来…一开始是错的。

vector =: i. 10   NB. Vector will be 0 1 2 3 4 5 6 7 8 9
(10) (0) } vector NB. Will yield 10 1 2 3 4 5 6 7 8 9

不久前,当我在CS课上第一次发现C语言时,看到指针的行为方式是非常奇怪的。我们只是编写程序并猜测它会做什么,直到它们得到正确的行为

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 (ISO/IEC 9899:1999, 6.4.6/3)和c++ (ISO/IEC 14882:2003, 2.5)有一个很少使用的特性,被C称为“有向图”,被c++称为“替代令牌”。它们不同于三trigraph,主要是因为包含它们的字符串字面量永远不会被不同地解释。

%:include <stdio.h>

int main() <%
    int a<:10:> = <%0%>;
    printf("Here's the 5th element of 'a': %d\n", a<:4:>);
    puts("Evil, eh? %:>");
    return 0;
%>

c++有更多,包括and, or, and not,它们被要求表现为&&,||和!C语言也有这些,但是需要包含<iso646.h>来使用它们,将它们视为宏而不是令牌。c++头文件<ciso646>实际上是一个空文件。

值得注意的是,GCC实现了对这种奇怪的语言特性的支持,但许多其他编译器在尝试编译上述代码段时都阻塞并死亡。