在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
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;
)
其他回答
在Python中:
>>> a[0] = "hello"
NameError: name 'a' is not defined
>>> a[0:] = "hello"
NameError: name 'a' is not defined
>>> a = []
>>> a[0] = "hello"
IndexError: list assignment index out of range
>>> a[0:] = "hello"
>>> a
['h', 'e', 'l', 'l', 'o']
这些切片分配也会给出相同的结果:
a[:] = "hello"
a[42:] = "hello"
a[:33] = "hello"
在Ruby中,你可以用heredocs做一些奇怪的事情。考虑:
a = <<ONE
This is one. #{<<TWO}
This is two. #{<<THREE}
This is three.
THREE
TWO
ONE
p a # => "This is one. This is two. This is three.\n\n\n"
在PHP中,你可以这样做:
System.out.print("hello");
在PHP中,“true”,“false”和“null”是常量,通常不能被重写。但是,随着PHP >=5.3中名称空间的引入,现在可以在除全局名称空间之外的任何名称空间中重新定义这些常量。这可能导致以下行为:
namespace {
define('test\true', 42);
define('test\false', 42);
define('test\null', 42);
}
namespace test {
var_dump(true === false && false === null); // is (bool) true
}
当然,如果希望真值为真,总是可以从全局名称空间导入真值
namespace test {
var_dump(\true === \false); // is (bool) false
}
我喜欢这类东西在JavaScript中很好的事实:
var futureDate = new Date(2010,77,154);
alert(futureDate);
结果是距离2010年第0个月的第0天77个月零154天,即2016年11月1日