在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“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
}
其他回答
在c++中,你可以做:
std::string my_str;
std::string my_str_concat = my_str + "foo";
但你不能:
std::string my_str_concat = "foo" + my_str;
操作符重载通常服从WTF。
这并不是一个真正的语言特性,而是一个实现缺陷:一些早期的Fortran编译器通过使用常量池来实现常量。所有参数都是通过引用传递的。如果你调用一个函数,例如。
f(1)
编译器会将常量池中常量1的地址传递给函数。 如果您为函数中的参数赋值,则会在程序中全局地更改该值(在本例中为1)。引起了一些挠头。
我很惊讶没有人提到大多数类c语言中丑陋的开关case实现
switch (someInt) {
case 1:
case 2: System.out.println("Forgot a break, idiot!");
case 3: System.out.println("Now you're doing the wrong thing and maybe need hours to find the missing break muahahahaha");
break;
default: System.out.println("This should never happen -,-");
}
好在新语言正确地实现了它。
在C中,a[b][C]与C [b[a]]完全相同。
Perl的许多内置变量:
$# — not a comment! $0, $$, and $? — just like the shell variables by the same name $ˋ, $&, and $' — weird matching variables $" and $, — weird variables for list- and output-field-separators $! — like errno as a number but strerror(errno) as a string $_ — the stealth variable, always used and never seen $#_ — index number of the last subroutine argument... maybe @_ — the (non)names of the current function... maybe $@ — the last-raised exception %:: — the symbol table $:, $^, $~, $-, and $= — something to do with output formats $. and $% — input line number, output page number $/ and $\ — input and output record separators $| — output buffering controller $[ — change your array base from 0-based to 1-based to 42-based: WHEEE! $} — nothing at all, oddly enough! $<, $>, $(, $) — real and effective UIDs and GIDs @ISA — names of current package’s direct superclasses $^T — script start-up time in epoch seconds $^O — current operating system name $^V — what version of Perl this is
还有很多这样的东西。点击这里阅读完整列表。