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

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


当前回答

在c++中,“虚”MI(多重继承)允许“菱形”类层次结构“工作”,这让我觉得奇怪和讨厌。

A:基类,例如:“对象” B, C:两者都(实际上或不是)源于对象和 D:起源于B和C

问题:“正常”继承导致D是2种不明确的A。“虚拟”MI将B的A和C的A折叠为一个共享基对象A。

所以,即使你的车轮是一个对象,你的左前轮是一个车轮,你的汽车继承了四种车轮,你的汽车仍然只是一种具有虚拟MI的对象。否则,你的汽车不是一个对象,而是4个车轮对象。

这是一种奖励糟糕的类设计、惩罚编译器编写者的语言特性,并让您在运行时怀疑对象到底在哪里——以及是否有任何虚拟MI行李放错了地方。

如果在类层次结构中确实需要菱形模式,可以使用常规MI和委托给单个A基的“AProxy”来完成。

A:基类,例如:“对象” AProxy:基类,与其他A绑定的构造 B:源自A C:来源于AProxy D:源自B和C(在构造时将B的A传递给C的AProxy)

对于那些真正喜欢diamond MI的人来说,这需要做更多的工作,而我们其他人则可以安心地使用一组更易于处理的语言特性。

其他回答

我一直在想为什么最简单的程序是:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

然而它可以是:

print "Hello World!"

也许这是为了吓唬计算机科学专业的学生……

英语中的虚拟语气。

等等,你是说编程语言吗?然后在C中使用(宏)绕过宏()的预处理器#定义。例如,如果有人使用#define free(…),(free)(…)将与free(…)不同。

我最喜欢的奇怪的C语言是5[“Hello World”],但因为已经发布了,我最喜欢的第二个奇怪的是Windows版本结构初始化黑客:

void someWindowsFunction() {
    BITMAPINFOHEADER header = {sizeof header};

    /* do stuff with header */
}

这条微妙的线条达到了以下效果:

Declares a BITMAPINFOHEADER structure Concisely sets the "size" member of the structure, without hardcoding a size constant (since many Window structures, including BITMAPINFOHEADER, follow the convention of specifying the size of the structure as the first member} Declares the version of the structure (since many Windows structures, including BITMAPINFOHEADER, identify their version by the declared size, following the convention that structures definitions are append-only) Clears all other members of the structure (a C standard behavior when a structure is incompletely initialized).

在Java中:

int[] numbers() {
  return null;
}

可以写成:

int numbers() [] {
  return null;
}

这可能已经被提到了,但是——

PHP对八进制值的处理:

$a = 07; // 7 (as it should be)
$b = 08; // 0 (would be an error in any sensible language)
$c = 018; // 1 (again, should have been an error)
$d = 0A; // error (as it should be)

请看这里:http://bugs.php.net/bug.php?id=29676

还要注意关于这个错误的评论——Derick称其为一个特性(引用“fix”表示),而不是一个错误,他声称它会“在脚本中使用数字的所有情况下显著降低PHP的速度”——但是,为什么PHP会引发一个0A错误呢?

我认为人们可以写一整本书来讲述PHP的古怪之处……