Strangest language feature

2018-12-31 02:51发布

What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?

Please only one feature per answer.

30条回答
忆尘夕之涩
2楼-- · 2018-12-31 03:28

In C, arrays can be indexed like so:

a[10]

which is very common.

However, the lesser known form (which really does work!) is:

10[a]

which means the same as the above.

查看更多
余欢
3楼-- · 2018-12-31 03:28

I struggled a bit about this:

1;

In perl, modules need to return something true.

查看更多
呛了眼睛熬了心
4楼-- · 2018-12-31 03:29

In Python:

>>> x=5
>>> 1<x<10
True
>>> 1<x<3
False

Not a WTF, but a useful feature.

查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 03:30

In JavaScript, the following construct

return
{
    id : 1234,
    title : 'Tony the Pony'
};

returns undefined is a syntax error due to the sneaky implicit semicolon insertion on the newline after return. The following works as you would expect though:

return {
    id : 1234,
    title : 'Tony the Pony'
};

Even worse, this one works as well (in Chrome, at least):

return /*
*/{
    id : 1234,
    title : 'Tony the Pony'
};

Here's a variant of the same issue that does not yield a syntax error, just silently fails:

return
    2 + 2;
查看更多
公子世无双
6楼-- · 2018-12-31 03:30

Algol pass by name (illustrated using C syntax):

int a[3] = { 1, 2, 3 };
int i = 1;

void f(int j)
{
    int k;
    k = j;  // k = 2
    i = 0;
    k = j;  // k = 1 (!?!)    
}

int main()
{
    f(a[i]);
}
查看更多
君临天下
7楼-- · 2018-12-31 03:31

My biggest most hated feature is any configuration file syntax which includes conditional logic. This sort of thing is rife in the Java world (Ant, Maven, etc. You know who you are!).

You just end up programming in a c**p language, with limited debugging and limited editor support.

If you need logic in your configuration the "Pythonic" approach of coding the configuration in a real language is much much better.

查看更多
登录 后发表回答