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.
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.
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.
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?
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.
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.
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.
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.
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.
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. :-)
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.
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.
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.
(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.
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.
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.
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.
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.
"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.
54 comments
[ 3.0 ms ] story [ 63.8 ms ] threadhttps://news.ycombinator.com/item?id=1601062
https://news.ycombinator.com/item?id=1032077
OTOH, by that same logic there should have been less JavaScript representation, because JavaScript is downright goofy.
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?
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...
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.
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?
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.
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...
Oh well. If you happen to find the funny but semi-legitimate one, I'd love to see it.
It's rumored to have been written by either Kernighan or Ritchie.
---
Get the number of elements in an array:
(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.
I personally prefer sizeof(*(arr)), which is the same as arr[0], but I suppose C++ lets you overload that as well.
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.
http://msdn.microsoft.com/en-us/library/aa266173(v=vs.60).as...