在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“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'; }
其他回答
在统一中,
GameObject.Find("MyObject")
将正常返回对象。然而,如果你这样做:
GameObject.Find("MyObject").active = false;
//do other stuff
if (GameObject.Find("MyObject").active)
{
//do stuff
}
然后您将得到一个空引用。在Unity iPhone中,这段代码通常在编辑器中正常工作,但在iPhone上运行时会导致SIGBUS。问题是GameObject.Find()将只定位活动对象,所以即使你只是检查它是否活动,你也会有效地调用if (null.active)。
为了使它正常工作,你必须在使它不活跃之前存储它。
GameObject obj = GameObject.Find("MyObject");
obj.active = false;
//do other stuff
if (obj.active)
{
//do stuff
}
可以说,这是一种更好的实践,但Unity处理非活动对象的方式通常是相当奇怪的。它似乎卸载了大部分非活动对象(纹理等),但不是全部,因此非活动对象仍然会消耗大量内存。
foo是什么数据类型?
SELECT TOP 1
NULL AS foo
INTO
dbo.bar
FROM
sys.columns --trivial
为什么所有东西都归零?
SELECT CAST('' AS int), CAST('' AS datetime), CAST('' AS float)
...除了这
SELECT CAST('' AS decimal)
Perl可以自动将26进制转换为10进制,如果你能在早上接受自己的话……
$ perl -E "say lc (@a='a'..'asdf')"
30530
在c++中,你可以做:
std::string my_str;
std::string my_str_concat = my_str + "foo";
但你不能:
std::string my_str_concat = "foo" + my_str;
操作符重载通常服从WTF。
Perl的许多内置变量:
$# — not a comment! $0, $$, and $? — just like the shell variables by the same name $ˋ, $&, and $' — weird matching variables $" and $, — weird variables for list- and output-field-separators $! — like errno as a number but strerror(errno) as a string $_ — the stealth variable, always used and never seen $#_ — index number of the last subroutine argument... maybe @_ — the (non)names of the current function... maybe $@ — the last-raised exception %:: — the symbol table $:, $^, $~, $-, and $= — something to do with output formats $. and $% — input line number, output page number $/ and $\ — input and output record separators $| — output buffering controller $[ — change your array base from 0-based to 1-based to 42-based: WHEEE! $} — nothing at all, oddly enough! $<, $>, $(, $) — real and effective UIDs and GIDs @ISA — names of current package’s direct superclasses $^T — script start-up time in epoch seconds $^O — current operating system name $^V — what version of Perl this is
还有很多这样的东西。点击这里阅读完整列表。