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

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


当前回答

作为一名NHibernate爱好者,当我从Smalltalk听到be时,我非常激动……如。

a become: b

它直接将a对象更改为b,这使得编写惰性初始化代理变得很简单,因为所有对a的引用现在都将引用b。非常简洁!

我认为这是一种奇怪的语言特征,因为据我所知,没有其他语言具有这种能力。

其他回答

c++(或Java)中没有封装的事实。任何对象都可以违反任何其他对象的封装,破坏它的私有数据,只要它是相同的类型。例如:

#include <iostream>
using namespace std;

class X
{
  public:
    // Construct by passing internal value
    X (int i) : i (i) {}

    // This breaks encapsulation
    void violate (X & other)
    {
        other.i += i;
    }

    int get () { return i; }

  private:
    int i;
};

int main (int ac, char * av[])
{
    X a(1), b(2), c(3);

    a.violate (c);
    b.violate (c);
    cout << c.get() << endl;    // "6"
}

在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"

大约在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)))

Commodore BASIC的命令快捷方式。基本上大多数命令都有一个缩写形式,通常是第一个字母+ (shift+第二个字母)。但是因为C64上的字符集默认都是大写的,这些命令看起来就像奇怪的符号。下面是一个hello world的简短例子:

也许有人有更好的例子,更有实质内容,但对于长程序来说,这看起来完全是荒谬的。

以下是缩略语列表:http://www.c64-wiki.com/index.php/BASIC_keyword_abbreviation

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

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

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

这句话让我很意外……