在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Haskell又说:
在Haskell中,你可以处理任意大小的文件,就好像它是一个简单的字符串。只有在实际使用字符串时,文件才会被读取。由于Haskell令人难以置信的懒惰,这样的程序将在恒定的空间中运行,而不管文件的大小:
main = interact (>>= \x -> if x == '\n' then "\r\n" else [x])
(这个程序将一个文件从stdin转换为stdout,并将LF替换为CRLF,交互函数将整个stdin输入到一个函数,并将输出移动到stdout。)
这种惰性也可能导致问题,因为如果关闭一个文件句柄,就不能完全shure,不管Haskell是否已经解析了其中的所有数据。
其他回答
在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);
或者类似的东西。你懂的…这是不可能的,我也不知道为什么……
Perl的CORE::开放和标准库具有面向对象的元素,用一个过程性接口掩盖: Open (my $fh, '>', 'foobar'); Open是一个构造函数,对my()返回的引用进行操作,并接受参数'>'和'foobar'。此外,这是一个受祝福的typeglob对象(意味着它不能在对象内部保存状态)。
关于我的perlmonks post IO::File vs CORE::的更多信息,请在这里打开:http://www.perlmonks.org/?node_id=763565
C预处理器及其用法。特别是预处理器元编程和使用预处理器生成可移植代码——完全是胡扯。
在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。这是个很棒的故事。
Forth的控制结构有些奇怪。首先,因为它是一种反向波兰符号语言,条件在IF之前,如:
x 0 = IF
现在,要关闭条件块,使用关键字THEN:
x 0 = IF ." Equals zero!" THEN
现在真正的WTF开始了。IF所做的是编译一个有条件的前向跳转,并将跳转偏移量的地址放在堆栈上。当找到THEN时,它从堆栈中弹出该地址,计算实际偏移量,然后编译它。另一方面,ELSE编译一个条件向前跳转,从堆栈中弹出一个地址,将一个新地址压入堆栈,计算弹出地址的偏移量,然后编译该偏移量。这意味着语法是这样的:
x 0 = IF ." Equals zero!" ELSE ." Not equal to zero!" THEN
第一个和第二个语句是这样编译的:
x LITERAL 0 = (0BRANCH) LITERAL offset SLITERAL" Equals zero!" (DOTQ)
x LITERAL 0 = (0BRANCH) LITERAL offset SLITERAL" Equals zero!" (DOTQ) BRANCH LITERAL offset SLITERAL" Not equal to zero!" (DOTQ)
更奇怪的是,这种行为并不隐藏。它是该语言的ANSI规范的一部分,可以通过构造自定义流控制结构或以有趣的方式组合它们来自由利用。例如,Forth的WHILE循环:
BEGIN x 10 < WHILE x 1+ to x REPEAT
BEGIN和WHILE之间的部分是任意代码,因此实际上可以在单个控制结构中让代码在条件测试之前和之后执行。这是故意的,但下面的内容虽然是允许的,但却不允许:
BEGIN DUP 2 > WHILE DUP 5 < WHILE DUP 1+ REPEAT 123 ELSE 345 THEN
它利用了每个控制流字的工作方式来组合两个WHILE语句,并为每个出口添加不同的后循环代码来引导。为了表明我不是在开玩笑,我只是从互联网上的一个代码中复制了这个小片段,并进行了一些小的修改来简化它。