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

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


当前回答

Perl的CORE::开放和标准库具有面向对象的元素,用一个过程性接口掩盖: Open (my $fh, '>', 'foobar'); Open是一个构造函数,对my()返回的引用进行操作,并接受参数'>'和'foobar'。此外,这是一个受祝福的typeglob对象(意味着它不能在对象内部保存状态)。

关于我的perlmonks post IO::File vs CORE::的更多信息,请在这里打开:http://www.perlmonks.org/?node_id=763565

其他回答

Java的访问修饰符对我来说是最近的一个WTF(因为我必须学习一点)。

显然,包比类层次结构更亲密。我不能定义对子类可见但对包中的其他类不可见的方法和属性。为什么我要将一个类的内部共享给其他类呢?

但是我可以定义对包内的每个类可见的属性和方法,但对包外的子类不可见。

不管我怎么想,我还是看不出其中的逻辑。切换访问修饰符,使受保护的行为就像它在c++中工作一样,并保持包的私有修饰符,这是有意义的。但现在不是了。

c++模板可以用来做一些奇怪的事情,最好的例子是“多维模拟字面量”,它使用模板来计算“绘制”形状的面积。下面的代码对于3x3矩形是有效的c++代码

#include"analogliterals.hpp"
using namespace analog_literals::symbols;

          unsigned int c = ( o-----o
                             |     !
                             !     !
                             !     !
                             o-----o ).area;

再举一个3D立方体的例子:

  assert( ( o-------------o
            |L             \
            | L             \
            |  L             \
            |   o-------------o
            |   !             !
            !   !             !
            o   |             !
             L  |             !
              L |             !
               L|             !
                o-------------o ).volume == ( o-------------o
                                              |             !
                                              !             !
                                              !             !
                                              o-------------o ).area * int(I-------------I) );

在Java中,

Int x = 010;

这将x赋值为8。

在Java中,任何前面带0的整数都被假定为八进制。

在x++ (Microsoft Dynamics AX):

1)需要在单独的行上使用分号(;)将变量声明与语句分开(至少在4.0版本之前)

    int i;
    int myArray[5];
    ;
    i = 1;

2)数组索引是基于1的,所以你不允许像in那样使用索引0(零)从数组中读取

    int myArray[5];
    ;
    print myArray[0];    // runtime error

这并不奇怪,但是你可以在赋值的左边使用0索引,比如in

    int myArray[5];
    ;
    myArray[2] = 102;
    myArray[0] = 100;    // this is strange
    print myArray[2];    // expcting 102?

会发生什么呢?数组被初始化为它的默认值,无论赋值中使用了什么值。上面的代码输出0(零)!

PHP对字符串中数值的处理。详见之前对另一个问题的回答,但简而言之:

"01a4" != "001a4"

如果你有两个包含不同数量字符的字符串,它们不能被认为是相等的。前导零很重要,因为它们是字符串而不是数字。

"01e4" == "001e4"

PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates "01e4" == "10000" as true because these are numbers with equivalent values. This is documented behaviour, it’s just not very sensible.