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

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


当前回答

INTERCAL可能是最奇怪的语言特征的最佳汇编。我个人最喜欢的是COMEFROM语句,它(几乎)与GOTO相反。

COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.

其他回答

在Ruby中,你可以用heredocs做一些奇怪的事情。考虑:

a = <<ONE
This is one. #{<<TWO}
This is two. #{<<THREE}
This is three.
THREE
TWO
ONE

p a # => "This is one. This is two. This is three.\n\n\n"

MySQL枚举,特别是他们的能力,使生活的地狱毫无准备的同事。

CREATE TABLE foo (
    ....
    dispatched ENUM('0','1') NOT NULL DEFAULT '0',
)

然后:

UPDATE TABLE foo SET ..., dispatched = 1;

哎呀,dispatch被设置为0,因为1没有被引用。这真的惹恼了为我编写代码的人;我现在使用普通的int。

在一个相关的注意事项,即使你添加一个空字符串选项到你的枚举,例如。

blah ENUM('','A','B') NOT NULL,

如果给blah赋了一个无效值,MySQL将使用一个秘密隐藏的空字符串值来表示无效值,这将很难与您自己添加的值区分开来。耶!

PHP作为一门完整的语言基本上是WTF。

语言定义(参见www.php.org)不是由语法或标准定义的,而是由一堆“你可以写这个例子”的部分(当然,你可以写其他东西,只是猜测泛化)定义的,诚实的用户说“但它做了这种古怪的事情……”。

在使用我们构建的PHP解析器时,我经常会遇到一些故障。这是最新的消息:

 "abc$A[define]def"

现在,PHP是PERL的一个(非常糟糕的)副本,因此它允许用隐式变量替换构造字符串。字符串中的$X表示“将$X的值插入字符串”,相当于“abc”。$ X。"def",其中"."是PHP的字符串连接操作符。

字符串中的$A[7]表示“将数组$A的第七个槽的值插入字符串”,相当于“abc”。美元[7]。“def”。

现在,语言(网站)清楚地说“定义”是一个关键字,你不能 只要你能找到合适的表达就用它。那么上面包含“define”的宝石做什么呢?抛出语法错误?不,那样说得通。

不,它实际上的意思是:

 "abc" . $A["define"] . "def"

只有当你在一个字符串的简单数组访问中写一个看起来像标识符(关键字或不是!)的东西时,它才会这样做。在语言的其他地方没有这种行为。 什么,写“abc$A["define"]def”是不合理的,所以PHP发明者不得不把它扔进去?得了吧。(更严重的是,“字符串中有复杂的数组访问”,当然它的工作方式不同。查看"abc{$A[define]}def";根据PHP网站,这是非法的。

(PHP数组是关联哈希表,因此根据名称查找数组(嗯,哈希表)成员并不是一个糟糕的想法)。

语言中充满了这样的陷阱。如果你喜欢“天哪,看看我今天在子程序下发现了什么蠕动的东西”,你应该切换到PHP。

在Javascript中,我认为以下是等价的:

a['title'] = "Syntactic sugar is good for yr teeth.";
a.title = "Syntactic sugar is good for yr teeth.";

Python for循环中的else。

来自Python文档:

for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print n, 'equals', x, '*', n/x
            break
    else:
        # loop fell through without finding a factor
        print n, 'is a prime number'

输出:

2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3