在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Python中:
>>> x=5
>>> 1<x<10
True
>>> 1<x<3
False
不是一个WTF,但是一个有用的特性。
其他回答
Perl充满了奇怪但整洁的特性。
If可以用在语句之前或之后,就像这样:
print "Hello World" if $a > 1;
if ($a > 1) { print "Hello World"; }
foreach也是如此:
print "Hello $_!\n" foreach qw(world Dolly nurse);
在Java中(这是一个导致赋值的if语句)
result = (Boolean condition) ? (if Boolean is true) : (if Boolean is false);
or
data Nat = Z|S Nat deriving Show
nattoInt Z = 0
nattoInt (S a) = 1 + nattoInt a
buildNat 0 = Z
buildNat a = S (buildNat (a - 1))
在Haskell…我仍然不太明白这是如何定义自然数的(我完全理解理论:-p)
在我看来,在c#中能够将超出范围的整数转换为枚举是非常奇怪的。想象一下这个枚举:
enum Colour
{
Red = 1,
Green = 2,
Blue = 3
}
现在,如果你写:
Colour eco;
eco = (Colour)17;
编译器认为这很好。还有运行时。
请看这里了解更多细节。
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.
PHP中的变量
PHP中一个奇怪的特性,它允许你从其他变量的内容中创建和分配变量(警告,未经测试的代码):
$a = 'Juliet';
$$a = 'awesome'; // assigns a variable named $Juliet with value 'awesome'
echo '$a'; // prints Juliet
echo '${$a}'; // prints awesome
echo '$Juliet'; // prints awesome
好吧,假设我们有这样的东西:
$bob = 'I\'m bob';
$joe = 'I\'m joe';
$someVarName = 'bob';
$$someVarName = 'Variable \'bob\' changed';
用各种间接的方式来找点乐子怎么样:
$juliet = 'Juliet is awesome!';
$func = 'getVarName'
echo '${$func()}'; // prints 'Juliet is awesome!'
function getVarName() { return 'juliet'; }