在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Javascript中的变量/函数声明:
var x = 1;
function weird(){
return x;
var x = 2;
}
Weird()返回undefined…即使任务从未发生,X也被“占用”了。
类似的,但也不是那么出乎意料
function weird2(){
var x;
return x();
function x(){ return 2 };
}
返回2。
其他回答
Perl充满了奇怪但整洁的特性。
If可以用在语句之前或之后,就像这样:
print "Hello World" if $a > 1;
if ($a > 1) { print "Hello World"; }
foreach也是如此:
print "Hello $_!\n" foreach qw(world Dolly nurse);
在c++中,“虚”MI(多重继承)允许“菱形”类层次结构“工作”,这让我觉得奇怪和讨厌。
A:基类,例如:“对象” B, C:两者都(实际上或不是)源于对象和 D:起源于B和C
问题:“正常”继承导致D是2种不明确的A。“虚拟”MI将B的A和C的A折叠为一个共享基对象A。
所以,即使你的车轮是一个对象,你的左前轮是一个车轮,你的汽车继承了四种车轮,你的汽车仍然只是一种具有虚拟MI的对象。否则,你的汽车不是一个对象,而是4个车轮对象。
这是一种奖励糟糕的类设计、惩罚编译器编写者的语言特性,并让您在运行时怀疑对象到底在哪里——以及是否有任何虚拟MI行李放错了地方。
如果在类层次结构中确实需要菱形模式,可以使用常规MI和委托给单个A基的“AProxy”来完成。
A:基类,例如:“对象” AProxy:基类,与其他A绑定的构造 B:源自A C:来源于AProxy D:源自B和C(在构造时将B的A传递给C的AProxy)
对于那些真正喜欢diamond MI的人来说,这需要做更多的工作,而我们其他人则可以安心地使用一组更易于处理的语言特性。
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.
我一直是PHP错误的忠实粉丝,当在一行中使用两个冒号时脱离上下文:
解析错误:语法错误,第3行/path/to/file/error.php中的T_PAAMAYIM_NEKUDOTAYIM异常
第一次遇到这种情况时,我完全被弄糊涂了。
在C或c++中,sizeof…参数的圆括号是可选的。假设参数不是类型:
void foo() {
int int_inst;
// usual style - brackets ...
size_t a = sizeof(int);
size_t b = sizeof(int_inst);
size_t c = sizeof(99);
// but ...
size_t d = sizeof int_inst; // this is ok
// size_t e = sizeof int; // this is NOT ok
size_t f = sizeof 99; // this is also ok
}
我一直不明白这是为什么!