在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
下面的c#代码抛出NullReferenceException而不是打印1:
static void SomeMethod(string format, params object[] args)
{
Console.WriteLine(args.Length);
}
static void Main(string[] args)
{
SomeMethod("blabla", null, "Ok here"); // print 2
SomeMethod("blabla", null); // exception
}
其他回答
Python 2。X演示了一个糟糕的列表理解实现:
z = 4
s = [z*z for z in range(255)]
print z
这段代码返回254。列表推导式的变量与上定义的变量冲突。
Python 3。x已经处理了这个特性,但是闭包仍然对外部变量使用动态链接,并在函数式python编程器中引入了许多wtf
def mapper(x):
return x*x
continuations = [lambda: mapper(x) for x in range(5)]
print( [c() for c in continuations])
此代码显然返回[16,16,16,16,16]。
我最喜欢的c++之一:
#include <iostream>
using namespace std;
int main()
{
cout << 3 << 5 << endl;
cout << (3 << 5) << endl;
return 0;
}
当然,这很容易解释,但它让刚开始编程的学生摸不着头脑!
Perl的$[(已弃用),这在之前关于通用Perl变量的另一篇文章中提到过,但它值得特别提到,并有更好的解释。对$[的更改仅限于当前范围。更多的信息以及如何使用它的含义;)可以在$[中找到,请访问http://www.perlmonks.org/index.pl/?node_id=480333
在早期版本的Visual Basic中,没有“Return”语句的函数只是“Return None”,没有任何编译器警告(或错误)。
这导致了最疯狂的调试会话,那时我必须每天处理这种语言。
INTERCAL可能是最奇怪的语言特征的最佳汇编。我个人最喜欢的是COMEFROM语句,它(几乎)与GOTO相反。
COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.