在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
从Ruby中的随机类继承:
class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample
...
end
(首次出现在Ruby的隐藏特性中)
其他回答
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.
Python的everything-is-really-a-reference有一个有趣的副作用:
>>> a = [[1]] * 7
>>> a
[[1], [1], [1], [1], [1], [1], [1]]
>>> a[0][0] = 2
>>> a
[[2], [2], [2], [2], [2], [2], [2]]
在JavaScript中:
var something = 12;
function nicelyCraftedFunction()
{
something = 13;
// ... some other code
// ... and in Galaxy far, far away this:
if( false ) // so the block never executes:
{
var something;
}
}
nicelyCraftedFunction(); // call of the function
通常你会期望某个变量的值是13。 但在JavaScript中不是这样——变量有函数作用域,所以后面的声明会影响上游的一切。
在使用C/ c++ /Java表示法的语言(如JS)中,你会期望变量具有块范围,而不是像这样…
因此,编译器甚至可以从最终生成的字节码中删除的死代码块仍然会对正常执行的其余代码产生副作用。
因此,在调用函数之后,某些东西仍然是12 -不变的。
让我们为所有试图废除保留词的语言(如PL/I)投票。
还有什么地方可以合法地写出这样有趣的表达:
IF IF THEN THEN = ELSE ELSE ELSE = THEN
(IF, THEN, ELSE是变量名)
or
IF IF THEN THEN ELSE ELSE
(IF为变量,THEN和ELSE为子程序)
Python for循环中的else。
来自Python文档:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
输出:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3