在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
一个意想不到的特性是,在C、c#、Ruby等语言中,枚举定义列表和数组初始化列表中的末尾逗号。
string[] foods = { "tofu", "grits", "cabbage", }
public enum ArtPeriod {
Modern,
Romantic,
Dada,
}
其他回答
反向波兰符号。这意味着实参在函数之前。换句话说,2加2就是2 +。
具有WTF特性的语言包括Forth、Postscript(是的,激光打印机的Postscript)和Factor。
我一直是PHP错误的忠实粉丝,当在一行中使用两个冒号时脱离上下文:
解析错误:语法错误,第3行/path/to/file/error.php中的T_PAAMAYIM_NEKUDOTAYIM异常
第一次遇到这种情况时,我完全被弄糊涂了。
我认为这实际上不是一个“语言特性”(C),我很可能在发布它时很无知,但我不知道为什么会发生这种情况,所以我会问。如果它被证明与一些奇怪的语言特征有关…这真的让我很不爽,所以这个地方是值得的。
int a = 0;
int *p = &a;
printf("%d, %d, %d.\n", *p, (*p)++, *p); // Outputs "1, 0, 0.\n" on MinGW's GCC 4.4.1
Why?
——编辑
刚拿到的,没什么大不了的。我能感觉到c++大师们现在在嘲笑我。我猜函数参数计算的顺序是未指定的,所以编译器可以自由地调用它们(我想我已经在boost的文档中读到过)。在本例中,实参语句是向后求值的,这可能反映了函数的调用约定。
C# has a feature called "extension methods", which are roughly analogous to Ruby mix-ins - Essentially, you can add a method to any pre-existing class definition (for instance, you oould add "reverse()" to String if you were so inclined). That alone is fine- The "Weird" part is that you can add these extension methods, with a method body and everything, to an interface. On the one hand, this can be handy as a way to add a single method to a whole swath of classes which aren't part of the same inheritance tree. On the other, you're adding fleshed out methods to interfaces, essentially breaking the very definition of an interface.
在MySQL中字符串比较是不区分大小写的。
> SELECT * FROM blah WHERE foo = 'BAR';
> SELECT * FROM blah WHERE foo = 'Bar';
> SELECT * FROM blah WHERE foo = 'bAr';
都是等价的。它们不仅会匹配任何看起来像'bar'的foo值(例如,如果foo = 'bar',它将匹配bar, bar, bar等)。