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

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


当前回答

在Forth中,任何不包含空格的东西都可以是标识符(包含空格的东西需要做一些工作)。解析器首先检查事物是否定义了,在这种情况下,它被称为单词,如果没有,则检查它是否为数字。没有关键字。

无论如何,这意味着人们可以重新定义一个数字来表示其他东西:

: 0 1 ;

它创建了单词0,由1组成,不管它在执行时是什么。反过来,它会导致以下结果:

0 0 + .
2 Ok

另一方面,定义可以接管解析器本身——这是可以做到的 通过评论词。这意味着Forth程序实际上可以在中途变成一个完全不同语言的程序。事实上,这是Forth推荐的编程方式:首先你写你想要解决问题的语言,然后你解决问题。

其他回答

大约在1977年,我在Lisp中添加了“format”函数,那时“printf”甚至还不存在(我是从与Unix相同的源:Multics复制的)。它一开始很无辜,但后来被一个接一个的特征填满了。当Guy Steele引入迭代和相关特性时,事情就失控了,这些特性被Common Lisp X3J13 ANSI标准所接受。下面的示例可以在Common Lisp The Language, 2nd Edition第22.3.3节中的表22-8中找到:

(defun print-xapping (xapping stream depth)
  (declare (ignore depth))
  (format stream
      "~:[{~;[~]~:{~S~:[->~S~;~*~]~:^ ~}~:[~; ~]~ ~{~S->~^ ~}~:[~; ~]~[~*~;->~S~;->~*~]~:[}~;]~]"
      (xectorp xapping)
      (do ((vp (xectorp xapping))
           (sp (finite-part-is-xetp xapping))
           (d (xapping-domain xapping) (cdr d))
           (r (xapping-range xapping) (cdr r))
           (z '() (cons (list (if vp (car r) (car d)) (or vp sp) (car r)) z)))
          ((null d) (reverse z)))
      (and (xapping-domain xapping)
           (or (xapping-exceptions xapping)
           (xapping-infinite xapping)))
      (xapping-exceptions xapping)
      (and (xapping-exceptions xapping)
           (xapping-infinite xapping))
      (ecase (xapping-infinite xapping)
        ((nil) 0)
        (:constant 1)
        (:universal 2))
      (xapping-default xapping)
      (xectorp xapping)))

在JavaScript中,void不是关键字,不是类型声明,也不是变量名,也不是函数,也不是对象。Void是一个前缀操作符,类似于-、——、++和!你可以给任何表达式加上前缀,这个表达式的值将是undefined。

它经常被用在bookmarklet和内联事件处理程序中,比如下面这个比较常见的例子:

<a href="javascript:void(0)">do nothing</a>

在这个例子中使用它的方式使它看起来像一个函数调用,而实际上它只是一种获得原始未定义值的过于聪明的方法。大多数人并没有真正理解JavaScript中void的真正本质,这可能会导致许多讨厌的bug和奇怪的意想不到的事情发生。

不幸的是,我认为void操作符是在JavaScript中获得未定义值的唯一真正保证的方法,因为未定义,正如在另一个回答中指出的,是一个可以重新赋值的变量名,而{}。a可以被Object.prototype.a = 'foo'打乱

更新:我想到了另一种生成undefined的方法:

(function(){}())

嗯,有点啰嗦,返回“undefined”是它的目的就更不清楚了。

在C中,a[b][C]与C [b[a]]完全相同。

对于那些不知道的人来说,PostScript实际上是一种编程语言。我已经有点疯狂了——我写了一个PostScript程序,计算曼德尔布罗特分形的细节非常高。它真的是可打印的PostScript,尽管它会崩溃很多打印驱动程序…

总之,从哪里开始PostScript…这里有一个例子:您实际上可以创建一个标识符为....的变量什么都没有。

() cvn 5 def %将数字5分配给…没有什么

PostScript是一种基于堆栈的语言。()将一个空字符串放入堆栈。cvn将其转换为一个名称(如果打印它,则为“/”,因为PS中的所有名称前面都有斜杠)。然后5def将值5赋给它。(%是注释字符)

你不能直接把它拿回来,例如,如果我说“/ print”,这将不会打印数字5。但你可以间接地把它拿回来:

() CVN负载打印%这将打印数字5

还有什么……PostScript将字典作为原生类型,您可以使用数组引用作为字典的键…但是REFERENCE才是键,而不是数组。所以:

/myDict 100 dict def

[0] dup myDict exch 42 put myDict exch get == %打印42

myDict [1] 42 put myDict [1] get %抛出未定义错误

编辑:哦,是的,还有一件有趣的事情……在Ghostscript提示下尝试以下操作:

1 array dup dup 0 exch put ==

分析!

在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。这是个很棒的故事。