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

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


当前回答

腮腺炎。WTF有很多特性,我选了一个if语句。(请注意,我在下面使用了一种相当冗长的编码风格,以适应那些不懂这门语言的人;真正的腮腺炎代码通常对外行来说更难以理解。)

if x>10 do myTag(x)    ; in MUMPS "tag" means procedure/function
else  do otherTag(x)

这类似于Java中的说法:

if (x > 10) {
  myMethod(x);
} else {
  otherMethod(x);
}

除了在MUMPS中,else语句在语法上不是if块的一部分,它是一个单独的语句,通过检查内置变量$TEST来工作。每次执行if语句时,它都会将$TEST设置为if语句的结果。else语句实际上意味着“如果$TEST为假,则执行该行剩余部分,否则跳转到下一行”。

这意味着如果x大于10,因此第一行叫做myTag,并且myTag包含if语句,那么else的行为不取决于它上面一行的if,而是取决于myTag内部的最后一个if !由于这个“特征”,MUMPS编码员通常被教导像这样编写以上代码以确保安全:

if x>10 do myTag(x) if 1
else  do otherTag(x)

第一行末尾的if 1确保在控制进行到下一行之前正确地设置了$TEST。(顺便说一下,这里的间距必须是这样的,在else后面有两个空格,在所有其他地方都有一个空格。间距是奇怪的,但至少一旦你理解了模式,它是非常正交的。)

其他回答

引用尼尔·弗雷泽的话(看那一页的末尾)

try {
    return true;
} finally {
    return false;
}

(在Java中,但在JavaScript和Python中行为显然是相同的)。其结果留给读者作为练习。

编辑:只要我们谈到这个问题,也要考虑到这一点:

try {
    throw new AssertionError();
} finally {
    return false;
}

在fortran中(当然是77,可能在95中也是如此),未声明的变量和以I到N开头的参数(“In”组)将是INTEGER,所有其他未声明的变量和参数将是REAL(源)。这与“在某些情况下可选的空白”相结合,导致了最著名的错误之一。

正如弗雷德·韦伯在1990年的《另类民间传说:计算机》一书中所说:

I worked at Nasa during the summer of 1963. The group I was working in was doing preliminary work on the Mission Control Center computer systems and programs. My office mate had the job of testing out an orbit computation program which had been used during the Mercury flights. Running some test data with known answers through it, he was getting answers that were close, but not accurate enough. So, he started looking for numerical problems in the algorithm, checking to make sure his tests data was really correct, etc. After a couple of weeks with no results, he came across a DO statement, in the form: DO 10 I=1.10 This statement was interpreted by the compiler (correctly) as: DO10I = 1.10 The programmer had clearly intended: DO 10 I = 1, 10 After changing the . to a , the program results were correct to the desired accuracy. Apparently, the program's answers had been "good enough" for the sub-orbital Mercury flights, so no one suspected a bug until they tried to get greater accuracy, in anticipation of later orbital and moon flights. As far as I know, this particular bug was never blamed for any actual failure of a space flight, but the other details here seem close enough that I'm sure this incident is the source of the DO story.

我认为这是一个很大的WTF,如果DO10I被作为DO10I,并且反过来,因为隐式声明被认为是类型REAL。这是个很棒的故事。

我有点纠结:

1;

在perl中,模块需要返回true。

在c++中,你可以从空指针调用静态方法——看!

class Foo {
  public:
    static void bar() {
      std::cout << "WTF!?" << std::endl;
    }
};

int main(void) {
  Foo * foo = NULL;
  foo->bar(); //=> WTF!?
  return 0; // Ok!
}

这句话让我很意外……

在Python中:

>>> a[0] = "hello"
NameError: name 'a' is not defined
>>> a[0:] = "hello"
NameError: name 'a' is not defined
>>> a = []
>>> a[0] = "hello"
IndexError: list assignment index out of range
>>> a[0:] = "hello"
>>> a
['h', 'e', 'l', 'l', 'o']

这些切片分配也会给出相同的结果:

a[:] = "hello"
a[42:] = "hello"
a[:33] = "hello"