在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
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中,字符串字母不能像在C中那样使用,你需要使用ord()和chr()来将数字转换为字符,反之亦然:"a" != 97,但ord("a") == 97。
不过,有一个例外:
for ($i = 'a'; $i < 'z'; $i++) {
print "$i ";
}
将打印字母a到y,就像你所期望的,就像它是C风格的数据类型一样。
但是,如果将测试条件更改为<=,它将不会像您想象的那样输出a到z,而是输出a到yz!(共打印676项)
同样,如果您将676项列表中“z”后面的“z”更改为“aa”,并再次将测试条件更改为<,您将只看到“a”被打印出来!不是像你想的那样从a到z。
如果你将增量变量改为$i+=2,它将再次只打印“a”!唯一的方法是按顺序使用$i++, $i++,现在它像预期的那样工作。
尽管如此,这是PHP中输出字母a-z组合的好方法,尽管实际使用起来非常困难。
Processing (processing.org)是一种基于Java的语言。简单来说,处理编译器是将特定于处理的语法转换为Java的Java预处理器。
由于语言的设计,它有一些惊喜:
Processing的类被编译成Java内部类,这会引起一些麻烦,比如私有变量并不是真正私有的
class Foo {
private int var = 0; // compiles fine
}
void setup() {
Foo f = new Foo();
print(f.var); // but does not causes compile error
}
同样缺少draw()函数会导致事件处理程序不被调用:
// void draw() {} // if you forgot to include this
void mousePressed() {
print("this is never called");
}
在Matlab中,以下内容可能会让你感到惊讶,特别是如果你习惯了Python:
>> not true
ans =
0 0 0 0
>> not false
ans =
0 0 0 0 0
这里有两个奇怪的特征。第一个是a b被解释为a('b'),因此非真被解释为not('true')。第二个奇怪的特征是没有任何字符返回0(大概是因为在matlab中没有false或true,只有0或1)。
大约在1977年,我在Lisp中添加了“format”函数,那时“printf”甚至还不存在(我是从与Unix相同的源:Multics复制的)。它一开始很无辜,但后来被一个接一个的特征填满了。当Guy Steele引入迭代和相关特性时,事情就失控了,这些特性被Common Lisp X3J13 ANSI标准所接受。下面的示例可以在Common Lisp The Language, 2nd Edition第22.3.3节中的表22-8中找到:
(defun print-xapping (xapping stream depth)
(declare (ignore depth))
(format stream
"~:[{~;[~]~:{~S~:[->~S~;~*~]~:^ ~}~:[~; ~]~ ~{~S->~^ ~}~:[~; ~]~[~*~;->~S~;->~*~]~:[}~;]~]"
(xectorp xapping)
(do ((vp (xectorp xapping))
(sp (finite-part-is-xetp xapping))
(d (xapping-domain xapping) (cdr d))
(r (xapping-range xapping) (cdr r))
(z '() (cons (list (if vp (car r) (car d)) (or vp sp) (car r)) z)))
((null d) (reverse z)))
(and (xapping-domain xapping)
(or (xapping-exceptions xapping)
(xapping-infinite xapping)))
(xapping-exceptions xapping)
(and (xapping-exceptions xapping)
(xapping-infinite xapping))
(ecase (xapping-infinite xapping)
((nil) 0)
(:constant 1)
(:universal 2))
(xapping-default xapping)
(xectorp xapping)))
在PHP中,如下:
<?php $foo = 'abc'; echo "{$foo";
是语法错误。
如果你真的想要{,后面跟着$foo的内容,你必须使用。:
<?php $foo = 'abc'; echo '{' . $foo;