News for hackers. Stuff that matters.
Re: Strangest language feature by edanm on Sunday March 24, 2013 @03:31PM (#5433206)
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.

Re: Strangest language feature by reeses on Sunday March 24, 2013 @04:02PM (#5433342)
Pointer arithmetic via syntactic sugar is definitely fun. I still love the occasional "variable index of a literal".
Re: Strangest language feature by mikeash on Sunday March 24, 2013 @05:52PM (#5433875)
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.
Re: Strangest language feature by edanm on Sunday March 24, 2013 @06:04PM (#5433947)
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...

Re: Strangest language feature by derobert on Monday March 25, 2013 @02:28PM (#5438693)
The answer on the deleted question (http://stackoverflow.com/questions/469696/what-is-your-most-...), by Josh Kelley, gives this:

---

Get the number of elements in an array:

    #define ITEMSOF(arr)    (sizeof(arr) / sizeof(0[arr]))
(0[arr] is identical to arr[0] for arrays but will intentionally fail if it's used against a C++ object that overloads operator[].)

He gives a C++ template version, too.

---

... 10k+ users on Stack Overflow can see deleted stuff. Or you can probably get them out of the CC data dumps (but that's a lot more work than you want, probably). Or maybe out of the data explorer.

Re: Strangest language feature by mikeash on Monday March 25, 2013 @02:40PM (#5438775)
Ah! I saw that #define but didn't see the bit about failing on C++ objects that overload operator[]. I don't do C++ much so that really didn't occur to me.

I personally prefer sizeof(*(arr)), which is the same as arr[0], but I suppose C++ lets you overload that as well.

Re: Strangest language feature by mikeash on Sunday March 24, 2013 @06:09PM (#5433983)
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.

Re: Strangest language feature by greyfade on Tuesday March 26, 2013 @06:11PM (#5446400)
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.

Re: Strangest language feature by edanm on Sunday March 24, 2013 @09:11PM (#5434708)
You're right - that is the normal way (although not commonly seen). I'll update if I find it.
Re: Strangest language feature by huhtenberg on Sunday March 24, 2013 @05:02PM (#5433575)
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
Re: Strangest language feature by niggler on Sunday March 24, 2013 @07:45PM (#5434396)
I wouldn't necessarily call this strange if you think about how this directly translates to assembly instructions.
Re: Strangest language feature by podperson on Sunday March 24, 2013 @10:54PM (#5435082)
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.
Re: Strangest language feature by smsm42 on Monday March 25, 2013 @02:17AM (#5435759)
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.
Re: Strangest language feature by podperson on Monday March 25, 2013 @05:43PM (#5439907)
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.
Re: Strangest language feature by vor_ on Sunday March 24, 2013 @01:58PM (#5432838)
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.
Re: Strangest language feature by andrewflnr on Sunday March 24, 2013 @02:08PM (#5432878)
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?

Re: Strangest language feature by derleth on Sunday March 24, 2013 @02:29PM (#5432948)
> 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.

Re: Strangest language feature by frou_dh on Sunday March 24, 2013 @04:04PM (#5433354)
A dozen people scramble to make that "original" observation any time Stack Overflow is mentioned anywhere.
Re: Strangest language feature by Dewie on Sunday March 24, 2013 @04:12PM (#5433380)
Open-ended and subjective questions tend to be more interesting than questions that have a definite answer.
Re: Strangest language feature by bkanber on Sunday March 24, 2013 @11:47AM (#5432358)
Conway's Game of Life in APL blew me away: http://www.youtube.com/watch?v=a9xAKttWgP4
Re: Strangest language feature by Vivtek on Sunday March 24, 2013 @05:17PM (#5433653)
Dear God. Now I have a quick fix for any time I feel like I need a dose of humility.
Re: Strangest language feature by ysangkok on Sunday March 24, 2013 @08:35PM (#5434563)
What's strange here? He's factual, explains everything he does. No mysticism here.
Re: Strangest language feature by pja on Sunday March 24, 2013 @11:08AM (#5432246)
Such a shame these kinds of questions are banned from StackOverflow these days. Takes all the fun out things...
Re: Strangest language feature by JohnLBevan on Sunday March 24, 2013 @11:25AM (#5432291)
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/
Re: Strangest language feature by pja on Sunday March 24, 2013 @11:27AM (#5432299)
I'm sure the StackOverflow anti-fun police will come for those too.
Re: Strangest language feature by podperson on Sunday March 24, 2013 @10:55PM (#5435089)
Perhaps the problem with SO in the long run may be that moderators are, in the end, people who seek to be moderators.
Re: Strangest language feature by ufmace on Sunday March 24, 2013 @01:42PM (#5432787)
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...

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?

Re: Strangest language feature by kvb on Sunday March 24, 2013 @02:27PM (#5432940)
String constants are interned at compile time. Dynamically created strings are typically not interned.
Re: Strangest language feature by LoneWolf on Sunday March 24, 2013 @05:00PM (#5433558)
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.
Re: Strangest language feature by ufmace on Sunday March 24, 2013 @05:56PM (#5433895)
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...

Re: Strangest language feature by jrajav on Sunday March 24, 2013 @10:37AM (#5432155)
Re: Strangest language feature by zxcdw on Sunday March 24, 2013 @11:41AM (#5432343)
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.
Re: Strangest language feature by georgemcbay on Sunday March 24, 2013 @04:00PM (#5433332)
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.

Re: Strangest language feature by mikeash on Sunday March 24, 2013 @05:53PM (#5433883)
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.
Re: Strangest language feature by swift on Sunday March 24, 2013 @02:39PM (#5432998)
I don't really feel either of those features are that 'strange'. Most of C++'s features are not bizarre considered in isolation. Their ugliness comes from the fact that they have surprising limitations, unpleasant syntax, and don't compose well.
Re: Strangest language feature by podperson on Sunday March 24, 2013 @10:57PM (#5435094)
Perhaps, as with PHP, there are simply too many obvious options.
Re: Strangest language feature by mylons on Sunday March 24, 2013 @11:47AM (#5432356)
I really like this one in C++: http://stackoverflow.com/a/2024573/710332
Re: Strangest language feature by jbert on Sunday March 24, 2013 @05:42PM (#5433828)
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.

Re: Strangest language feature by victorh on Sunday March 24, 2013 @06:15PM (#5434018)
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.
Re: Strangest language feature by Liongadev on Sunday March 24, 2013 @10:51AM (#5432201)
So much javascript in this stackoverflow thread.
Re: Strangest language feature by teleological on Sunday March 24, 2013 @06:35PM (#5434111)
"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.
Re: Strangest language feature by mehrzad on Sunday March 24, 2013 @02:05PM (#5432868)
Surprised to not see Erlang there.
Re: Strangest language feature by Argorak on Sunday March 24, 2013 @02:59PM (#5433079)
Erlang is rusty and sometimes very different, but rarely strange.
Re: Strangest language feature by andrewflnr on Sunday March 24, 2013 @03:26PM (#5433180)
Speak for yourself ;). Erlang's conditionals are weird, by any mainstreamish standard. And that's ok, they're sorta cool once you get them.
Re: Strangest language feature by Argorak on Monday March 25, 2013 @02:44AM (#5435795)
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?

Re: Strangest language feature by andrewflnr on Tuesday March 26, 2013 @09:48PM (#5447275)
Fair enough. I don't consider ML and prolog to be "mainstreamish", and I'm defining "strange" to mean "unusual".
Re: Strangest language feature by reeses on Sunday March 24, 2013 @04:00PM (#5433331)
It's strange in the sense that Willy Wonka's fizzy lifting drinks are strange in a factory with oompa loompas and chocolate rivers. Any good experience is going to change your brain and feel strange at first. :-)
Re: Strangest language feature by mehrzad on Sunday March 24, 2013 @03:24PM (#5433171)
I think "[x]." returning a certain character when 7<x<256 is pretty strange. There's probably a reason for this, but I'm pretty new to the language.
Re: Strangest language feature by kragen on Sunday March 24, 2013 @06:17PM (#5434032)
It's that Erlang doesn't have strings or characters. It has string syntax on input, which turns into lists of small integers, and also on output. But under the covers, those apparent strings are just lists of integers.
Re: Strangest language feature by hrydgard on Sunday March 24, 2013 @06:10PM (#5433985)
Re: Strangest language feature by epmatsw on Sunday March 24, 2013 @06:42PM (#5434143)
Oh god. I've seen way to many bugs as a result of this stupid feature.
Re: Strangest language feature by JohnLBevan on Sunday March 24, 2013 @11:21AM (#5432282)
I think Hume may have argued with the 'Cogito ergo sum' statement.
Re: Strangest language feature by protomyth on Sunday March 24, 2013 @02:59PM (#5433080)
Control / Conditionals in Icon and anything in MUMPS