在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
很久以前,我曾经用BUT子句构建过一种语言。
其他回答
在C或c++中,sizeof…参数的圆括号是可选的。假设参数不是类型:
void foo() {
int int_inst;
// usual style - brackets ...
size_t a = sizeof(int);
size_t b = sizeof(int_inst);
size_t c = sizeof(99);
// but ...
size_t d = sizeof int_inst; // this is ok
// size_t e = sizeof int; // this is NOT ok
size_t f = sizeof 99; // this is also ok
}
我一直不明白这是为什么!
在JavaScript中:
2 == [2]
// Even stranger
2 == [[[2]]]
// And down-right nutty
var a = { "abc" : 1 };
a[[[["abc"]]]] === a["abc"]; // this is also true
幸运的是,stackoverflow.com网站上善良的人们向我解释了这一切: 为什么2 ==[2]在JavaScript?
ruby中的隐含变量\constants和可变常量
在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中,你可以做到:
my $test = "Hello World";
substr($test, 0, 5) = "Goodbye";
print $test;
这在其他语言中可行吗?