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

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


当前回答

VBScript的With block:

With xml.appendChild(xml.createElement("category"))
  .setAttribute("id",id)
  .setAttribute("keywords",keywords)
  With .appendChild(xml.createElement("item"))
    .setAttribute("count",count)
    .setAttribute("tip",tip)
    .appendChild(xml.createTextNode(text))
  End With
End With

其他回答

我不敢相信这里还没有这个:JSF属性访问。

在JSF UI标签中,你可以将服务器端对象的属性值引用到接口中:

<h:inputText value="#{myObject.property}></h:inputText>

问题是Java不支持属性,所以你必须编写以get和set开头的方法,以便将UI对象链接到服务器上的“属性”。

public void setProperty(String property){...}
public String getProperty(){...}

当我第一次学习JSF时,这让我感到困惑,我仍然认为它值得使用wtf。尽管在Java实现对c#风格属性的支持之前,确实没有其他方法可以做到这一点。

我很惊讶居然没有人提到Visual Basic的7个循环结构。

For i As Integer = 1 to 10 ... Next
While True ... End While
Do While True ... Loop
Do Until True ... Loop
Do ... Loop While True
Do ... Loop Until True
While True ... Wend

因为粘!你面前的条件实在是太复杂了!

在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;
}

(只有当人们对数组、指针和操作符感到困惑时才会出现这种困惑。)

在c++中,你可以做:

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

但你不能:

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

操作符重载通常服从WTF。

这是我的两分钱。在c++中:

int* t = new int(15);
delete t;