在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
PHP
PHP对实例变量和方法的重载处理不一致。考虑:
class Foo
{
private $var = 'avalue';
private function doStuff()
{
return "Stuff";
}
public function __get($var)
{
return $this->$var;
}
public function __call($func, array $args = array())
{
return call_user_func_array(array($this, $func), $args);
}
}
$foo = new Foo;
var_dump($foo->var);
var_dump($foo->doStuff());
转储$var是有效的。即使$var是私有的,__get()也会被任何不存在或不可访问的成员调用,并返回正确的值。这不是doStuff()的情况,它失败于:
Fatal error: Call to private method Foo::doStuff() from context ”.”
我认为其中很多都是在c风格的语言中工作的,但我不确定。
Pass a here document as a function argument: function foo($message) { echo $message . "\n"; } foo(<<<EOF Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc blandit sem eleifend libero rhoncus iaculis. Nullam eget nisi at purus vestibulum tristique eu sit amet lorem. EOF ); You can assign a variable in an argument list. foo($message = "Hello"); echo $message; This works because an assignment is an expression which returns the assigned value. It’s the cause of one of the most common C-style bugs, performing an assignment instead of a comparison.
Python
在Python中,可变的默认函数参数会导致意想不到的结果:
def append(thing, collection=[]):
collection.append(thing)
return collection
print append("foo")
# -> ['foo']
print append("bar")
# -> ['foo', 'bar']
print append("baz", [])
# -> ['baz']
print append("quux")
# -> ['foo', 'bar', 'quux']
空列表是在函数定义时初始化的,而不是在调用时初始化的,因此对它的任何更改都会在函数调用之间保持不变。
MySQL的大小写敏感性
MySQL有非常不寻常的区分大小写的规则:表区分大小写,列名和字符串值不区分大小写:
mysql> CREATE TEMPORARY TABLE Foo (name varchar(128) NOT NULL);
DESCRIBE foo;
ERROR 1146 (42S02): Table 'foo' doesn't exist
mysql> DESCRIBE Foo;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(128) | NO | | NULL | |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.06 sec)
mysql> INSERT INTO Foo (`name`) VALUES ('bar'), ('baz');
Query OK, 2 row affected (0.05 sec)
mysql> SELECT * FROM Foo WHERE name = 'BAR';
+------+
| name |
+------+
| bar |
+------+
1 row in set (0.12 sec)
mysql> SELECT * FROM Foo WHERE name = 'bAr';
+------+
| name |
+------+
| bar |
+------+
1 row in set (0.05 sec)
其他回答
这可能已经被提到了,但是——
PHP对八进制值的处理:
$a = 07; // 7 (as it should be)
$b = 08; // 0 (would be an error in any sensible language)
$c = 018; // 1 (again, should have been an error)
$d = 0A; // error (as it should be)
请看这里:http://bugs.php.net/bug.php?id=29676
还要注意关于这个错误的评论——Derick称其为一个特性(引用“fix”表示),而不是一个错误,他声称它会“在脚本中使用数字的所有情况下显著降低PHP的速度”——但是,为什么PHP会引发一个0A错误呢?
我认为人们可以写一整本书来讲述PHP的古怪之处……
我很惊讶居然没有人提到Visual Basic的7个循环结构。
For i As Integer = 1 to 10 ... Next
While True ... End While
Do While True ... Loop
Do Until True ... Loop
Do ... Loop While True
Do ... Loop Until True
While True ... Wend
因为粘!你面前的条件实在是太复杂了!
在c++中,你可以从空指针调用静态方法——看!
class Foo {
public:
static void bar() {
std::cout << "WTF!?" << std::endl;
}
};
int main(void) {
Foo * foo = NULL;
foo->bar(); //=> WTF!?
return 0; // Ok!
}
这句话让我很意外……
APL(除了ALL),在一行中编写任何程序的能力。
例:在APL中,康威的生命游戏一行:
替代文本 http://catpad.net/michael/APLLife.gif
如果这句台词都不是WTF,那什么都不是!
这是一个视频
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)。