在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
学习PowerShell时发现:
试着猜一下结果数组是什么样的:
$a = 1, 2
$b = 1, 2+3
$c = 1, 2*3
答案:
1, 2
1, 2, 3
1, 2, 1, 2, 1, 2
哎哟!它动摇了我对PowerShell及其开发人员的信心。
其他回答
javascript:
parseInt('06'); // 6
parseInt('08'); // 0
交替:在许多语言中的事物之间交替:
boolean b = true;
for(int i = 0; i < 10; i++)
if(b = !b)
print i;
乍一看,b怎么可能不等于它自己呢? 这实际上只会打印奇数
在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。这是个很棒的故事。
其他奇怪的事情:
在c++中,覆盖一个虚方法会隐藏该方法的所有其他重载。在Java中,这种情况不会发生。这很烦人。例如:http://codepad.org/uhvl1nJp
在c++中,如果基类有一个公共虚方法foo(),子类有一个私有方法foo(),这个私有方法会覆盖另一个方法! 这样,只需将子类对象指针强制转换为父类对象指针,就可以在类外部调用私有方法。这不应该是可能的:这违反了封装。新方法不应被视为对旧方法的重写。例如:http://codepad.org/LUGSNPdh
在PHP中,你可以定义函数来接受类型化参数(例如,对象是某个接口/类的子类),讨厌的是,在这种情况下,你不能使用NULL作为实际的参数值。 例如:http://codepad.org/FphVRZ3S
在C语言中,sizeof操作符不计算其实参。这允许编写看起来错误但实际上是正确的代码。例如,给定类型T,调用malloc()的惯用方法是:
#include <stdlib.h>
T *data = NULL;
data = malloc(sizeof *data);
这里,*data在sizeof操作符中不被求值(data是NULL,所以如果它被求值,就会发生糟糕的事情!)。
这使得人们能够编写令人惊讶的代码,无论如何,对于新手来说。请注意,没有哪个头脑正常的人会这么做:
#include <stdio.h>
int main()
{
int x = 1;
size_t sz = sizeof(x++);
printf("%d\n", x);
return 0;
}
输出1,而不是2,因为x永远不会增加。
对于sizeof的一些真正的乐趣/困惑:
#include <stdio.h>
int main(void)
{
char a[] = "Hello";
size_t s1 = sizeof a;
size_t s2 = sizeof ("Hi", a);
printf("%zu %zu\n", s1, s2);
return 0;
}
(只有当人们对数组、指针和操作符感到困惑时才会出现这种困惑。)