在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Perl中,对象只是被祝福的引用,所以在运行时改变对象的类是小菜一碟:
package Foo;
sub new { bless {}, $_[0] }
package Bar;
package main;
my $foo = Foo->new;
ref($foo); # => "Foo"
bless $foo, 'Bar';
ref($foo); # => "Bar"
我很惊讶其他语言不能做到这一点。多么有用的功能啊!
其他回答
再给JavaScript投一票:
parseInt('08') == 0
因为任何前导为0的数都被解释为八进制(奇怪),无效的八进制数被计算为0(坏)。我是在8月份发现这一点的,当时我几个月没碰过的代码自己崩溃了。事实证明,它本可以在10月份自行修复。
八进制支持显然已被弃用,所以未来几代的javascript将不会有这个仪式。
我一直在想为什么最简单的程序是:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
然而它可以是:
print "Hello World!"
也许这是为了吓唬计算机科学专业的学生……
在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。这是个很棒的故事。
在Perl中,可以通过修改一个类的@ISA数组来改变它的继承链,这让我很惊讶。
package Employee;
our @ISA = qw(Person);
# somwhere far far away in a package long ago
@Employee::ISA = qw(Shape);
# Now all Employee objects no longer inherit from 'Person' but from 'Shape'
大约20年前,当我最后一次涉足腮腺炎时,实现有一些奇怪的局限性。当主机MUMPS变得越来越流行时,MUMPS传统上是一种自托管语言:计算机语言、操作系统和数据库在一个包中。
腮腺炎主要是关于它的数据库。本质上,这是一个巨大的多维哈希表,由B*树支持,可以快速访问。语言和数据库之间也没有任何障碍:如果您想在那里存储一些东西,只需在变量前面加上一个符号,表明它将被持久化到备份存储中。
另一方面,文件系统几乎不存在,对它的支持就更少了。我们唯一能做的就是把一个程序从文件中加载到内存中,然后把内存中的内容发送回文件。最好在加载前清空缓冲区,否则它会和之前的东西混在一起。
因此,考虑到它的自托管性质和极其恶劣的文件系统,人们可能会想知道这些程序是如何编辑的。事实上,编辑器是用MUMPS本身编写的——那么,编辑器如何在不重写自己的情况下将程序存储在内存中呢?
好吧,诀窍在于能够以源代码的形式执行变量的内容。然后,编辑器将自己加载到变量中,在变量中执行自己,清除内存,然后在内存中加载、保存和编辑文件,一直从变量中执行。
除此之外,所有命令都可以被缩短为它们的第一个字母(除了Z命令,缩短为两个字母,主要处理文件系统),还有一个奇怪的事实,比如IF (I)设置了一个变量,然后由ELSE (E)查询——当然,可以被任何介入的I或程序本身覆盖。转念一想,我觉得整个语言就是个垃圾。然而,它有一种奇怪的吸引力。