

 Strangest language feature - vanwilder77
http://stackoverflow.com/questions/1995113/strangest-language-feature/1995117

======
edanm
The closest I am to being famous is this question on SO, to which I gave the
#1 answer. I'd been waiting years to get that piece of trivia off my chest,
and it was oh-so-rewarding. The answer was that you can use arrays like so:
5[arr], not just the usual way (arr[5])

Funny enough, several people gave interesting places where this kind of syntax
is a good thing. Goes to show that even meaningless pieces of fun on SO still
end of enriching the reader.

~~~
mikeash
What places is this syntax a good thing? I read through the comments on your
answer, but every single one that claimed this was either pointless (e.g. 0[x]
instead of (x)[0]) or not even an example of the trick in question (e.g.
"blah"[index] is not this). I was hoping for something interesting, thus my
question.

~~~
edanm
Honestly, I wrote this because I remembered that somebody had a use for this.
A funny use, but a semi-legitimate one none the less.

Unfortunately, I can't really find it now - there is a link to a question
which claims to have a use for this syntax, but it's been removed.

There are these 2 comments: "Don't forget "Hello World"[i]. Or i["Hello
World"] – Richard Pennington Jan 3 '10 at 15:12"

"Or, more usefully, "0123456789abcdef"[x & 0xf] "

Their basic point is that you can use this trick to find the i'th character in
a string. Which I guess is useful sometimes...

~~~
mikeash
Except that "0123456789abcdef"[x & 0xf] is just normal array dereferencing
syntax, not the backwards kind you pointed out. The backwards version would be
(x & 0xf)["0123456789abcdef"], which isn't really useful, I think.

Oh well. If you happen to find the funny but semi-legitimate one, I'd love to
see it.

~~~
greyfade
It was used in one of the early winners of the IOCCC:
<http://www.ioccc.org/1984/anonymous.c>

It's rumored to have been written by either Kernighan or Ritchie.

------
huhtenberg
For C it should most certainly be the ability to interleave the switch-case
and a block statement as per Duff's device:

    
    
        register n = (count + 7) / 8;
        switch(count % 8) {
        case 0: do {    *to = *from++;
        case 7:         *to = *from++;
        case 6:         *to = *from++;
        case 5:         *to = *from++;
        case 4:         *to = *from++;
        case 3:         *to = *from++;
        case 2:         *to = *from++;
        case 1:         *to = *from++;
            } while(--n > 0);
        }
    

[0] <http://en.wikipedia.org/wiki/Duff%27s_device>

~~~
niggler
I wouldn't necessarily call this strange if you think about how this directly
translates to assembly instructions.

~~~
podperson
It's pretty strange nonetheless that it compiles at all. Underlying it is that
a switch statement isn't a proper control structure but syntax sugar for goto.

~~~
smsm42
while() loops are really syntax sugar for goto too. And whole C is arguably
just a syntax sugar for assembler :) That's why we use it.

~~~
podperson
They're not syntax sugar for goto the way switch is though. You can't pull
stunts like that with two intermingled while loops because a while loop
operates on a single block.

------
vor_
It's amusing how nearly all of the most interesting and popular questions I
see on StackOverflow are closed by moderators for not being good or
constructive.

~~~
andrewflnr
It makes perfect sense in light of StackOverflow's stated reason for existing,
which is to answer closed-ended questions.

Can we please not turn this thread into a complaining session about SO's
policies?

~~~
derleth
> Can we please not turn this thread into a complaining session about SO's
> policies?

No, because every website has to be all things to all people, even if it means
the destruction of the few things it was actually good at.

------
bkanber
Conway's Game of Life in APL blew me away:
<http://www.youtube.com/watch?v=a9xAKttWgP4>

~~~
Vivtek
Dear God. Now I have a quick fix for any time I feel like I need a dose of
humility.

------
pja
Such a shame these kinds of questions are banned from StackOverflow these
days. Takes all the fun out things...

~~~
JohnLBevan
There's Code Golf and Programmers to cater for some of these things - but I'm
a little blurry on the rules of exactly what the boundaries of each site are.
<http://codegolf.stackexchange.com/> <http://programmers.stackexchange.com/>

~~~
pja
I'm sure the StackOverflow anti-fun police will come for those too.

~~~
podperson
Perhaps the problem with SO in the long run may be that moderators are, in the
end, people who seek to be moderators.

------
ufmace
Found a few interesting bits there. A reference to a Quiz about C# and object
equality:
[http://blogs.msdn.com/b/jmstall/archive/2005/03/06/386064.as...](http://blogs.msdn.com/b/jmstall/archive/2005/03/06/386064.aspx)

The last one makes me wonder a bit. Given local int variable x, what does this
evaluate to?

(object) x.ToString() == (object) x.ToString()

When I run it, I get false, and the author says that it's false, because we're
doing reference comparison of two independently created objects. But I thought
strings were interned in .NET, so if you independently create 2 strings with
the same contents, then they are actually the same object, which would make
this true?

~~~
LoneWolf
I would say that it is because each time ToString() is called a new string is
created, and at least in the Java I believe that is what will happen,
comparison will be false because they are not the same instance.

~~~
ufmace
It's a bit trickier than that due to string interning. That's where there is
only one instance framework-wide of a string of a particular value. Try to
create a second string with the same value as one you have created already,
and you get back a reference to the first string instance.

As an example:

(object)"XYZ" == (object)"XYZ"

returns true. So does:

(object) String.Intern(x.ToString()) == (object) String.Intern(x.ToString())

The trick is to know exactly which strings are interned, and when. I initially
thought that all strings were, but it seems that string literals in code are
always interned, and dynamically-created strings are mostly not interned
automatically, but can be manually.

<http://en.wikipedia.org/wiki/String_interning> [http://msdn.microsoft.com/en-
us/library/system.string.intern...](http://msdn.microsoft.com/en-
us/library/system.string.intern.aspx)

------
jrajav
Previous discussions:

<https://news.ycombinator.com/item?id=1601062>

<https://news.ycombinator.com/item?id=1032077>

------
zxcdw
Weirdly many people loathe C++ for it's weird parts and complexity and all,
but there's very little of C++ in that thread. I honestly expected people to
run circles around templates and operator overloading.

~~~
georgemcbay
I almost feel like C++ gets a pass because people just expect it to have weird
fucked up stuff. People expect weirdness out of it thus wouldn't be surprised
by examples.

OTOH, by that same logic there should have been less JavaScript
representation, because JavaScript is downright goofy.

~~~
mikeash
Sort of like how if you asked about weird stuff in biology, you won't get a
lot of Platypus answers, even though the Platypus is really weird.

------
mylons
I really like this one in C++: <http://stackoverflow.com/a/2024573/710332>

------
jbert
Not in the sense of "wrong", but in terms of difficulty of comprehension, for
me it's call/cc in scheme:

<http://en.wikipedia.org/wiki/Call-with-current-continuation>

The main thing which helped me get some kind of grip on it was having worked
through longjmp/setjump in C some time ago.

~~~
victorh
If you check with Oleg, it actually is wrong! Delimited continuations are
strictly more powerful _and_ composable. We're supposed to be using
shift/reset or prompt/abort.

------
Liongadev
So much javascript in this stackoverflow thread.

------
teleological
"Context" in Perl. Values undergo implicit conversion when assigned depending
on whether the LValue receiving the assignment is a scalar or a list type.
Perl functions can detect context at runtime (using "wantarray") and may vary
behavior for different contexts.

------
mehrzad
Surprised to not see Erlang there.

~~~
Argorak
Erlang is rusty and sometimes very different, but rarely strange.

~~~
andrewflnr
Speak for yourself ;). Erlang's conditionals are weird, by any mainstreamish
standard. And that's ok, they're sorta cool once you get them.

~~~
Argorak
As someone who started programming in ML and later in Prolog, they are far
from weird.

Other than that, they are not a "strange language feature", they don't behave
in unexpected or unclear ways.

While `!` is certainly a very special piece of erlang, its also not strange as
well, as in say.... Ruby flipflops?

~~~
andrewflnr
Fair enough. I don't consider ML and prolog to be "mainstreamish", and I'm
defining "strange" to mean "unusual".

------
hrydgard
On Error Resume Next.

[http://msdn.microsoft.com/en-
us/library/aa266173(v=vs.60).as...](http://msdn.microsoft.com/en-
us/library/aa266173\(v=vs.60\).aspx)

~~~
epmatsw
Oh god. I've seen way to many bugs as a result of this stupid feature.

------
JohnLBevan
I think Hume may have argued with the 'Cogito ergo sum' statement.

------
protomyth
Control / Conditionals in Icon and anything in MUMPS

