在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

VBScript中的括号标识符

VBScript有所谓的方括号标识符,这些标识符定义在方括号中,如下所示:

[Foo]

实际上,它们非常方便,因为它们允许您以保留字命名变量和例程,调用第三方对象的方法,其名称是保留字,并且还可以在标识符中使用几乎任何Unicode字符(包括空格和特殊字符)。但这也意味着你可以和他们一起玩:

[2*2] = 5

[Здравствуй, мир!] = [Hello, world!]

[] = "Looks like my name is an empty string, isn't that cool?"

For[For[i=0]=[0]To[To[To[0]
  [Next[To]([For[i=0])=[For[i=0]
Next

另一方面,括号标识符可能是一个陷阱,以防你在这样的语句中忘记引号:

If MyString = "[Something]" Then

因为If MyString = [Something] Then是一个完全合法的语法。(这就是为什么必须使用具有语法高亮显示功能的IDE !)

更多关于括号标识符的信息,请参阅Eric Lippert的博客:

VBScript琐事:括号标识符和保留字不兼容 VBScript测试答案,第六部分

其他回答

来自边远吗?

在MySQL中字符串比较是不区分大小写的。

> SELECT * FROM blah WHERE foo = 'BAR';
> SELECT * FROM blah WHERE foo = 'Bar';
> SELECT * FROM blah WHERE foo = 'bAr';

都是等价的。它们不仅会匹配任何看起来像'bar'的foo值(例如,如果foo = 'bar',它将匹配bar, bar, bar等)。

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)

JavaScript八进制转换“特性”是一个很好的了解:

parseInt('06') // 6
parseInt('07') // 7
parseInt('08') // 0
parseInt('09') // 0
parseInt('10') // 10

详情请点击这里。

c++最恼人的解析:

struct S
{
    S() {} //default constructor
};

int main() {

    S s(); // this is not a default construction, it declares a function named s that takes no arguments and returns S.
}