在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Haskell:
let 2 + 2 = 5 in 2 + 2
收益率5。
其他回答
PHP
$ php -r '::'
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
WTF ? http://en.wikipedia.org/wiki/Scope_resolution_operator
为什么不说意外的T_SCOPE_RESOLUTION_OPERATOR ?
我有点纠结:
1;
在perl中,模块需要返回true。
我几年前使用的一个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并返回它。然后将此返回值转换回四个字节。
当我第一次遇到它时,它非常神秘。几乎我传递给某些函数的每个数字都被解释为零!
在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);
或者类似的东西。你懂的…这是不可能的,我也不知道为什么……
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.