在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
从Ruby中的随机类继承:
class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample
...
end
(首次出现在Ruby的隐藏特性中)
其他回答
Python的三元操作符
在Python中,C三元操作符(c++示例:bool isNegative = i < 0 ?True: false;)可用作语法糖:
>>> i = 1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is positive'
>>> i = -1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is negative!'
这并不奇怪,而是一种特征。奇怪的是,与C中的顺序(条件?答:b)。
Javascript中有很多东西会让你流泪。
局部变量的作用域,举个简单的例子:
function foo(obj)
{
for (var n = 0; n < 10; n++)
{
var t; // Here is a 't'
...
}
t = "okay"; // And here's the same 't'
}
在Perl中,对象只是被祝福的引用,所以在运行时改变对象的类是小菜一碟:
package Foo;
sub new { bless {}, $_[0] }
package Bar;
package main;
my $foo = Foo->new;
ref($foo); # => "Foo"
bless $foo, 'Bar';
ref($foo); # => "Bar"
我很惊讶其他语言不能做到这一点。多么有用的功能啊!
C + + 1 xλ的:
[] (int x) { std::cout << x << std::endl; } ();
它们可能被滥用在一些奇怪的语法中:
[](){}();[]{[]{}();}();
这是完全有效的c++ 1x。
Go的伪常数Iota:
type ByteSize float64
const (
_ = iota; // ignore first value by assigning to blank identifier
KB ByteSize = 1<<(10*iota); MB; GB; TB; PB; YB;
)