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

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


当前回答

Perl的sub没有真正的参数列表,只有@_数组。同时,sub会自动变平传入的参数。

我不明白为什么这是一个持久的功能;这反映了几年前我不得不在TI-86 BASIC上做的事情,因为这门语言没有足够的特色。

其他回答

在Java中从文本文件中读取一行。

BufferedReader in = null;
try {
   in = new BufferedReader(new FileReader("filename"));
   String str;
   str = in.readLine();
   if (str != null) {
      ...
   } 
} catch (IOException e) {
   ...
} finally {
   try {
      if (in != null) {
         in.close();
      }
   } catch (IOException e) {}
}

啊。虽然我承认这并不奇怪……只是邪恶。: -)

更短、更习惯的版本:

try {
   BufferedReader in = new BufferedReader(new FileReader("filename"));
   try {
       String str = in.readLine();
       while (str != null) {
          str = in.readLine();
       } 
    } finally {
        in.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}

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

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

: 0 1 ;

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

0 0 + .
2 Ok

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

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

转变;

有时你会在perl方法的第一行中看到它来读取self指针

下面是一个关于python的例子:

>>> print 07
7
>>> print 08
  File "<stdin>", line 1
    print 08
           ^
SyntaxError: invalid token

那不是很美吗?

尤其当你想到人类是如何写日期的时候,你会觉得很不周到,这有以下影响:

datetime.date(2010,02,07) # ok
datetime.date(2010,02,08) # error!

(原因是0x被解释为八进制,所以打印010打印8!)