在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Malbolge编程语言的完整版本:http://en.wikipedia.org/wiki/Malbolge
其他回答
在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);
或者类似的东西。你懂的…这是不可能的,我也不知道为什么……
JavaScript日期全是WTF。
var d = new Date("1/1/2001");
var wtfyear = d.getYear(); // 101 (the year - 1900)
// to get the *actual* year, use d.getFullYear()
var wtfmonth = d.getMonth(); // 0
// months are 0-based!
在C语言中,
int x = 1;
int y = x++ + ++x;
printf("%d", y);
是模棱两可的,打印什么取决于编译器。编译器可以在计算++x之前或在语句的末尾存储x++的新值。
下面的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语言中,sizeof操作符不计算其实参。这允许编写看起来错误但实际上是正确的代码。例如,给定类型T,调用malloc()的惯用方法是:
#include <stdlib.h>
T *data = NULL;
data = malloc(sizeof *data);
这里,*data在sizeof操作符中不被求值(data是NULL,所以如果它被求值,就会发生糟糕的事情!)。
这使得人们能够编写令人惊讶的代码,无论如何,对于新手来说。请注意,没有哪个头脑正常的人会这么做:
#include <stdio.h>
int main()
{
int x = 1;
size_t sz = sizeof(x++);
printf("%d\n", x);
return 0;
}
输出1,而不是2,因为x永远不会增加。
对于sizeof的一些真正的乐趣/困惑:
#include <stdio.h>
int main(void)
{
char a[] = "Hello";
size_t s1 = sizeof a;
size_t s2 = sizeof ("Hi", a);
printf("%zu %zu\n", s1, s2);
return 0;
}
(只有当人们对数组、指针和操作符感到困惑时才会出现这种困惑。)