在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在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。这是个很棒的故事。
其他回答
С#:
var a = Double.Parse("10.0", CultureInfo.InvariantCulture); // returns 10
var b = Double.Parse("10,0", CultureInfo.InvariantCulture); // returns 100
在不变区域性中,逗号不是小数点符号,而是组分隔符。
据我所知,对于一些地区的新手程序员来说,这是一个常见的错误。
COMEFROM是我见过的最奇怪,也可能是最没用的语言功能。
其次是三元运算符,因为它违反了优化的第一条规则。它带来的危害大于它解决的问题。它的危害更大,因为它使代码可读性更差。
并不是一个真正的语言功能,但有趣/很棒的功能使用是Duff的设备。
我喜欢Smalltalk中缺少运算符优先级
2 * 3 + 4 * 5 = 6 + 4 * 5 = 10 * 5 = 50
而不是
2 * 3 + 4 * 5 = 6 + 4 * 5 = 6 + 20 = 26
这是由于smalltalk的对象性质和消息从左向右传递的事实。如果消息*以数字3作为参数发送给2,则该消息的响应为6。太棒了,如果你觉得邪恶,你甚至可以用猴子来修补它。
我肯定会给Perl提供多个可怕的例子:
if(!$#var)
or
if($mystring =~ m/(\d+)/) {
在c#中,为什么这是不合法的?
public class MyClass<T>
where T: Enum
{
}
能够在Enum上添加扩展方法以及Func<T>是非常酷的,其中T将是您正在扩展的Enum,以便您可以对该Enum进行类型推断。
回复评论:是的,你可以扩展一个实际的枚举,但这里有区别:
你可以这样做:
public static void DoSomethingWithEnum(this Enum e)
{
//do whatever
}
但是如果你想用你的方法获取一个Func,它将是你的enum的相同类型:
public static void DoSomethingWithEnum<T>(this T e, Func<T,bool> func )
where T: Enum
{
//do whatever
}
这样,你就可以像这样调用你的方法:
DayOfWeek today = DayOfWeek.Monday;
today.DoSomethingWithEnum(e => e != DayOfWeek.Sunday);
或者类似的东西。你懂的…这是不可能的,我也不知道为什么……