在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Java的访问修饰符对我来说是最近的一个WTF(因为我必须学习一点)。
显然,包比类层次结构更亲密。我不能定义对子类可见但对包中的其他类不可见的方法和属性。为什么我要将一个类的内部共享给其他类呢?
但是我可以定义对包内的每个类可见的属性和方法,但对包外的子类不可见。
不管我怎么想,我还是看不出其中的逻辑。切换访问修饰符,使受保护的行为就像它在c++中工作一样,并保持包的私有修饰符,这是有意义的。但现在不是了。
其他回答
一个意想不到的特性是,在C、c#、Ruby等语言中,枚举定义列表和数组初始化列表中的末尾逗号。
string[] foods = { "tofu", "grits", "cabbage", }
public enum ArtPeriod {
Modern,
Romantic,
Dada,
}
C和c++中的三联体。
int main() {
printf("LOL??!");
}
这将打印LOL|,因为trigraph ??!转换为|。
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.
Perl:
可以编写一个完全由标点符号组成的程序。
这是怎么回事?!
这不是一个奇怪的功能,事实上,如果你仔细想想,它是完全有意义的,但还是给了我一个WTF时刻。
在c++(和c#)中,基类的子类不能访问基实例上的私有和受保护成员。
class Base {
protected:
m_fooBar;
};
class Derived: public Base {
public:
void Test(Base& baseInstance) {
m_fooBar=1; //OK
baseInstance.m_fooBar = 1; //Badness
//This, however is OK:
((Derived&)baseInstance).m_fooBar = 1; //OK
}
};