在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

在统一中,

GameObject.Find("MyObject")

将正常返回对象。然而,如果你这样做:

GameObject.Find("MyObject").active = false;
//do other stuff
if (GameObject.Find("MyObject").active)
{
    //do stuff
}

然后您将得到一个空引用。在Unity iPhone中,这段代码通常在编辑器中正常工作,但在iPhone上运行时会导致SIGBUS。问题是GameObject.Find()将只定位活动对象,所以即使你只是检查它是否活动,你也会有效地调用if (null.active)。

为了使它正常工作,你必须在使它不活跃之前存储它。

GameObject obj = GameObject.Find("MyObject");
obj.active = false;
//do other stuff
if (obj.active)
{
    //do stuff
}

可以说,这是一种更好的实践,但Unity处理非活动对象的方式通常是相当奇怪的。它似乎卸载了大部分非活动对象(纹理等),但不是全部,因此非活动对象仍然会消耗大量内存。

其他回答

在MOD_REWRITE

RewriteRule ^([a-zA-Z0-9_-]+)\.php$ $1/ [R,NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ $1\.php [NC,L]

将会导致:

"file.php > file > file.php > file > file.php > file >  ..."

最后:

Error 500 Too Many Redirects

(一般来说,我发现编辑.htaccess文件和构造一个正常运行的正则表达式一样乏味。)

c#的默认继承模型赢得了我的投票:

public class Animal
{
    public string Speak() { return "unknown sound" ; }
}

public class Dog : Animal
{
    public string Speak() { return "Woof!" ; }
}

class Program
{
    static void Main( string[] args )
    {
        Dog aDog = new Dog() ;
        Animal anAnimal = (Animal) aDog ;

        Console.WriteLine( "Dog sez '{0}'" , aDog.Speak() ) ;
        Console.WriteLine( "Animal sez '{0}'" , anAnimal.Speak() ) ;

        return ;
    }
}

运行程序得到如下结果:

狗叫“汪!” 动物说“未知的声音”

获得这种行为应该要求程序员走出程序员的道路。子类实例不会因为被上转换为它的超类型而停止存在。相反,你必须显式地请求预期的(几乎总是想要的)结果:

public class Animal
{
    public virtual string Speak() { return "unknown sound" ; }
}

public class Dog : Animal
{
    public override string Speak() { return "Woof!" ; }
}

我最喜欢的奇怪的C语言是5[“Hello World”],但因为已经发布了,我最喜欢的第二个奇怪的是Windows版本结构初始化黑客:

void someWindowsFunction() {
    BITMAPINFOHEADER header = {sizeof header};

    /* do stuff with header */
}

这条微妙的线条达到了以下效果:

Declares a BITMAPINFOHEADER structure Concisely sets the "size" member of the structure, without hardcoding a size constant (since many Window structures, including BITMAPINFOHEADER, follow the convention of specifying the size of the structure as the first member} Declares the version of the structure (since many Windows structures, including BITMAPINFOHEADER, identify their version by the declared size, following the convention that structures definitions are append-only) Clears all other members of the structure (a C standard behavior when a structure is incompletely initialized).

在c++中,你可以做:

std::string my_str;
std::string my_str_concat = my_str + "foo";

但你不能:

std::string my_str_concat = "foo" + my_str;

操作符重载通常服从WTF。

JavaScript真值表:

''        ==   '0'           // false
0         ==   ''            // true
0         ==   '0'           // true
false     ==   'false'       // false
false     ==   '0'           // true
false     ==   undefined     // false
false     ==   null          // false
null      ==   undefined     // true
" \t\r\n" ==   0             // true

资料来源:Doug Crockford