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

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


当前回答

在ruby/python/c中,你可以像这样连接字符串:

a = "foo" "bar"
print a # => "foobar"

其他回答

红宝石拖鞋。条件语句中的"…"和".."并不总是范围操作符:

(0..20).each do |x|
  if ((x%10) == 5)..((x%10) == 5)
    print "#{x} "
  end
end

(0..20).each do |x|
  if ((x%10) == 5)...((x%10) == 5)
    print "#{x} "
  end
end

这将输出:

5 15
5 6 7 8 9 10 11 12 13 14 15

. .检查每一遍的两个语句,…每次只检查“on”或“off”语句(取决于触发器状态)。它们是从awk和sed偷来的。

Matz在“Ruby编程语言”中写道:“人字拖是Ruby中一个相当晦涩的特性,可能最好避免……”

任何东西都会自动将任何类名和成员名复数或单数。

例如,Linq-to-Sql

腓backticks

从http://www.php.net/manual/en/language.operators.execution.php

PHP支持一种执行操作符:反撇号(' ')。注意,这些不是单引号!PHP将尝试作为shell命令执行反勾号的内容;输出将被返回(即,它不会简单地转储到输出;它可以赋值给一个变量)。

$output = `ls -al`;
echo "<pre>$output</pre>";

在代码中发现“instead of”是“相当容易的”。

这也很有趣:

经过一番折腾,我得出结论,反勾运算符(和shell_exec)的返回缓冲区有限。我的问题是,我正在处理一个超过50万行的文件,收到的回复远远超过10万行。短暂的停顿之后,我收到了大量来自grep的关于管道关闭的错误。

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

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

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

然而它可以是:

print "Hello World!"

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