在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
我很惊讶没有人提到大多数类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 -,-");
}
好在新语言正确地实现了它。
其他回答
我几年前使用的一个Fortran编译器有一个有趣的特性:(A)数字先存储在高字节;(b)数字按地址传递给子程序;(c)没有在编译时检查长度。
所以你可以写一个这样的程序:(如果我把语法弄乱了,请原谅。我已经很久没有写Fortran了。)
INTEGER*2 FUNCTION TIMESTWO (INTEGER*2 N)
RETURN N*2
... THEN CALL THIS SOMEWHERE WITH A LONG INTEGER ...
INTEGER*4 I, J
I=42
J=TIMESTWO(I)
J的最终值是…零!
为什么?因为传入的值是4个字节,但被调用的函数只查看前两个字节。由于前两个是0,它将0乘以2并返回它。然后将此返回值转换回四个字节。
当我第一次遇到它时,它非常神秘。几乎我传递给某些函数的每个数字都被解释为零!
这个古老的PHP宠儿本身并不完全是WTFish,但作用域解析错误是许多开发人员看到的事情之一,值得给一些WTF的爱:
$class = new StdClass();
$class::test();
PHP Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM on line 3
在Java中(这是一个导致赋值的if语句)
result = (Boolean condition) ? (if Boolean is true) : (if Boolean is false);
or
data Nat = Z|S Nat deriving Show
nattoInt Z = 0
nattoInt (S a) = 1 + nattoInt a
buildNat 0 = Z
buildNat a = S (buildNat (a - 1))
在Haskell…我仍然不太明白这是如何定义自然数的(我完全理解理论:-p)
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
还有很多这样的东西。点击这里阅读完整列表。
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.