在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
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'; }
其他回答
Python 2. x
>>>True = False
>>>True
False
你真的可以让一个人变得疯狂。
到目前为止,我遇到过的最奇怪的功能是BASIC方言中的“RETURN n”语句(不记得是哪一种了,这是大约28年前的事了)。“n”是可选的,默认为1。它可以是一个正数或负数,指示下一个执行的是与调用GOSUB相关的哪一行。
例如,下面将输出“30”:
10 GOSUB 200
20 PRINT "20"
30 PRINT "30"
100 END
200 RETURN +2
当我必须把一个用这种奇怪的BASIC语言编写的程序翻译成FORTRAN时,我遇到了这个问题。BASIC程序相当多地使用了这个特性,根据不同的条件返回不同的语句,我花了一段时间来理解逻辑流。一旦我理解了它,我就可以编写一个更简单的程序版本。不用说,更简单的FORTRAN版本比原来的BASIC程序bug更少。
在ruby/python/c中,你可以像这样连接字符串:
a = "foo" "bar"
print a # => "foobar"
Haskell's use of Maybe and Just. Maybe a is a type constructor that returns a type of Just a, but Maybe Int won't accept just an Int, it requires it to be a Just Int or Nothing. So in essence in haskell parlance Just Int is about as much of an Int as an apple is an orange. The only connection is that Just 5 returns a type of Maybe Interger, which can be constructed with the function Just and an Integer argument. This makes sense but is about as hard to explain as it can theoretically be, which is the purpose of haskell right? So is Just really JustKindaLikeButNotAtAll yea sorta, and is Maybe really a KindaLooksLikeOrIsNothing, yea sorta again.
-- Create a function that returns a Maybe Int, and return a 5, which know is definitly Int'able
> let x :: Maybe Int; x = 5;
<interactive>:1:24:
No instance for (Num (Maybe Int))
arising from the literal `5' at <interactive>:1:24
Possible fix: add an instance declaration for (Num (Maybe Int))
In the expression: 5
In the definition of `x': x = 5
> Just 5
Just 5
it :: Maybe Integer
-- Create a function x which takes an Int
> let x :: Int -> Int; x _ = 0;
x :: Int -> Int
-- Try to give it a Just Int
> x $ Just 5
<interactive>:1:4:
Couldn't match expected type `Int' against inferred type `Maybe t'
In the second argument of `($)', namely `Just 5'
In the expression: x $ Just 5
In the definition of `it': it = x $ Just 5
祝你好运读到这篇文章,我希望它是正确的。
在PHP中:
echo 'foo' == 0; // echos '1'
echo 'foo' == true; // echos '1'
echo 0 == true; // echos '0'
$foo = 'foo';
echo $foo['bar'] // echos 'f'
PHP有一些最烦人的类型强制转换…