在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在x++ (Microsoft Dynamics AX):
1)需要在单独的行上使用分号(;)将变量声明与语句分开(至少在4.0版本之前)
int i;
int myArray[5];
;
i = 1;
2)数组索引是基于1的,所以你不允许像in那样使用索引0(零)从数组中读取
int myArray[5];
;
print myArray[0]; // runtime error
这并不奇怪,但是你可以在赋值的左边使用0索引,比如in
int myArray[5];
;
myArray[2] = 102;
myArray[0] = 100; // this is strange
print myArray[2]; // expcting 102?
会发生什么呢?数组被初始化为它的默认值,无论赋值中使用了什么值。上面的代码输出0(零)!
其他回答
在PHP中:
<?php
$o = new stdClass;
echo count($o);
?>
..打印1。一直不知道为什么。
为什么c#的List<T>.AddRange()不让我添加T的子类型的元素?阀门列表< T > () ! 微软只需要多写一行代码:
public void AddRange<S>(
IEnumerable<S> collection
) where S : T
这并不是一个奇怪的特性,但从类型安全的角度来看,它确实令人恼火:c#中的数组协方差。
class Foo { }
class Bar : Foo { }
class Baz : Foo { }
Foo[] foo = new Bar[1];
foo[0] = new Baz(); // Oh snap!
我相信这是从Java继承而来的(双关语)。
在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语言中,数组可以像这样被索引:
a[10]
这很常见。
然而,鲜为人知的形式(真正有效!)是:
10[a]
这与上面的意思相同。