17 ms·
Strangest Language Features
- bitwize 15y agoCOBOL's ALTER statement. As if COBOL weren't WTFy enough, the statement ALTER X TO PROCEED TO Y changes any subsequent GOTO X statement to go to Y instead. Meaning it's anybody's guess where a given GO TO, considered in isolation, goes to!
- Someone 15y agoI upvoted you because it is the first thing I think of when I think of this question, but in Cobol's defense, self-modifying code (http://en.wikipedia.org/wiki/Self-modifying_code http://en.wikipedia.org/wiki/Self-modifying_code) was fairly normal at the time COBOL was conceived. For a nice example from a later date, look at the GetChar subroutine on Commodore Basic. It loaded a byte from a hard-coded address, and then updated that address (hm, probably in the reverse order, since, IIRC, it shared code with GotChar, which returned the last value GetChar returned)
- xd 15y agoPHP; converts array('123' => 0); into array(123 => 0); Which I came across as a problem when working with mongodb as it will only except strings as keys (not sure if this is enforced by PHP and/or mongo). Luckily you can create a class with numeric properties and pass that to mongodb which will then be treated as strings i.e $array = new stdClass; $array->{123} = 0;
- derleth 15y ago> only except strings A strange feature of the English language is that this doesn't work; you have to use 'accept' here.
- darklajid 15y agoSecond SO repost. Previously discussed with > 80 comments here: https://news.ycombinator.com/item?id=1601062 https://news.ycombinator.com/item?id=1601062
- danso 15y agoI have to admit that I upvoted the post because I had never seen this before, and the previous discussion is nearly 1.5 years old. That said....how did this get resubmitted? They both have the exact same URL ---- # edit: I see...one URL is http://stackoverflow.com/questions/1995113/ http://stackoverflow.com/questions/1995113/ The other has a slug in it: http://stackoverflow.com/questions/1995113/strangest-language-feature http://stackoverflow.com/questions/1995113/strangest-languag... I thought HN had some kind of redirect-autofollow that sniffed this kind of thing out? Or at least that's why I assumed hitting "submit" can result in a long delay, even before you're told that the title is > 80 (I wish there were a javascript validator for title length, while I'm pondering things...)
- chc 15y agoNope, even just appending a slash to a URL will get you past the dupe filter. Even beyond that, though, the filter only looks at recently accessed posts, so something with that long a gap between reposts almost certainly wouldn't get flagged.
- danso 15y agoWow...not a single SO response that said Ruby? I guess that's a good thing for us Rubyists?
- cobrausn 15y agoActually, there is, just not as many upvotes as the others. http://stackoverflow.com/a/2026849 http://stackoverflow.com/a/2026849 Basically, it is possible to inherit a random class in ruby. class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample ... end
- raganwald 15y agoI wouldn’t call that a strange feature, just a surprising consequence of an un-strange feature, namely that inheritance involves expressions and that classes are first-class entities. Folks from certain other languages would be just as surprised to find out that you can alias classes by assigning them, return them from functions, and so on.
- judofyr 15y agoSomeone else is probably going to mention flip-flops, so let me show you "defined?": a = 1 defined?(a) # => "local-variable" defined?(p) # => "method" defined?(nothing) # => nil defined? is a syntax-construct (not a method) which tries to tell you if its "parameter" is defined. The rules for this are rather interesting. class String def print p self self end end Okay, String#print is just a helper method to prove my point. defined?("hello".print) # => "method" defined?("hello".print.strip) # => prints out "hello", then returns "method" defined?("hello".print.stripp) # => prints out "hello", then returns nil Okay, so defined? may actually call methods for you to figure out if it's "defined" or not. What if we try something more complex: defined?(if true then "hello".print.strip end) # => "expression" Wait, what? This does not call the print method. In fact, this check is done entirely at parse-time. "defined?(if true then … end)" will always return "expression". In fact, anything more complex than local variables, method calls or constant names will just return "expression". Well, except for "defined?" though; defined? can't check itself - it's a syntax error. Another intresting "feature": If any exception is raised, the whole expression is considered undefined: class String; def print; raise; end; end defined?("hello".print.strip) # => nil
- deleted 15y ago[deleted]
- deleted 15y ago[deleted]
- atlbeer 15y agoThis is an evil bug in Javascript parseInt('06') // 6 parseInt('07') // 7 parseInt('08') // 0 parseInt('09') // 0 parseInt('10') // 10 parseInt should really return NaN for '08', '09' etc
- gcp 15y agoOctal notation, with an invalid number. Makes perfect sense. It's not a bug, behaves like specified: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt https://developer.mozilla.org/en/JavaScript/Reference/Global... If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed. [...] For this reason always specify a radix when using parseInt.
- chc 15y agoI really don't see how it makes perfect sense for a value which is not by any stretch of imagination a valid octal number to be interpreted as an octal number. Imagine if `parseInt('foo')` gave 38274 — would you also think that made perfect sense? Also, that document says the behavior is nonstandard, so I don't even see how you can say it's behaving as specified.
- gcp 15y agoI really don't see how it makes perfect sense for a value which is not by any stretch of imagination a valid octal number to be interpreted as an octal number. Imagine if `parseInt('foo')` gave 38274 — would you also think that made perfect sense? The page specifically says that if radix is unspecified, then "If the input string begins with "0", radix is eight (octal)." The page also says "If the first character cannot be converted to a number, parseInt returns NaN." so your example is also perfectly defined to return NaN (and thus bogus). Also, that document says the behavior is nonstandard, so I don't see even how you can say it's behaving as specified. It's a weird wording in that webpage, because ECMAScript 3 allows it: "When radix is 0 or undefined and the string's number begins with a 0 digit not followed by an x or X, then the implementation may, at its discretion, interpret the number either as being octal or as being decimal. Implementations are encouraged to interpret numbers in this case as being decimal." ECMAScript 5 no longer allows it. Given that your code isn't necessarily processed by an ECMAScript 5 compliant JavaScript engine, it's indeed behaving as specified. Maybe instead of "non-standard" a better word would have been "behavior varies between implementations".
- niyazpk 15y agoIn JavaScript: (NaN === NaN) is false (NaN !== NaN) is true
- raganwald 15y agoThis is isomorphic to NULLs in some languages (I think SQL behaves this way). Basically, NaN isn’t some specific thing, it’s an indication of something that isn’t a number. So two things that aren’t numbers can’t be thought to be equal to each other, they might be radically different things. If there’s a WTF, it’s that NaN === NaN is either true or false. If I were writing a language, it might be undecideable. But if you end up with: Undecideable === Undecideable //=> Undecideable You are opening up the “Hopelessly Egocentric Null” can of worms: https://github.com/raganwald/homoiconic/blob/master/2009-02-02/hopeless_egocentricity.md https://github.com/raganwald/homoiconic/blob/master/2009-02-...
- masklinn 15y ago> I think SQL behaves this way Yes. It's also how NaNs are supposed to work according to IEEE-754. The only reason why it can be strange in javascript is that it's pretty common to encounter nans in JS as the language tries very hard not to throw errors and likes nans a lot (so `parseInt('foo')` returns NaN instead of `null` or an error), whereas it's very rare in other languages. Pretty much all languages have nans, and all nans behave this way. Here it is in Python: >>> float('nan') nan >>> float('nan') == float('nan') False >>> float('nan') != float('nan') True >>> a = float('nan') >>> a == a False >>> import decimal >>> decimal.Decimal('NaN') Decimal('NaN') >>> decimal.Decimal('NaN') == decimal.Decimal('NaN') False >>> a = decimal.Decimal('NaN') >>> a == a False edit: now that I think about it, there is a difference: in SQL, all operations involving `NULL` (including comparisons) return `NULL`, so `NULL = NULL` => `NULL` and `NULL != NULL` => `NULL`.
- chc 15y agoThat's not a JavaScript feature; it's part of the IEEE 754 standard for floating point numbers. NaNs are unordered. Every language that provides standard floating point numbers does it that way.
- protomyth 15y agoIcon's conditional statements were pretty odd http://en.wikipedia.org/wiki/Icon_(programming_language) http://en.wikipedia.org/wiki/Icon_(programming_language)
- ww520 15y agoThe Multi-Dimensional Analog Literals template in C++ is amazing.
- masklinn 15y agoSmalltalk's "cascading" operator is a strange (in the nicest way possible, it's mostly strange that nobody else has it) feature: a pretty popular style in OO languages these days is "fluent interfaces", in which methods return their `this` and — through method chaining — developers can call several methods on the same object while limiting verbosity. This style is of course possible in smalltalk: foo.bar().baz() becomes foo bar baz and if `bar` returns `self`/`this`, `baz` will be called on the same object. However, this has the issue that method implementors have to choose between fluent interfaces and returning useful values, as the fluency is decided by the return value domain for the method. Smalltalk sidesteps this by having an operator `;` which essentially means "the message after this operator is sent to the same object as the message before this operator". As a result, in the example above you could have `bar` returning the height of the empire state building, and you'd still get the exact same result by writing: foo bar ; baz (a message cascade returns the value returned by the last message) Smalltalk was/is not fond of literal syntaxes, so its collection APIs (as far as adding to them goes anyway) look a lot like Java's, yet you could write things in a terser manner by using cascading: c := (ArrayList new) add: 3 ; add: 4 ; add: 5 ; add: 6 ; yourself. is equivalent to: c := ArrayList new. c add: 3. c add: 4. c add: 5. c add: 6. (note `yourself`, which does exactly what it says: it returns its `self`) (and yes I know you can combine Java's anonymous subclasses and instance initializers to get something similar, in the specific case of instantiating non-final collections).
- munificent 15y ago> it's mostly strange that nobody else has it For what it's worth, we're working on adding it to Dart. I don't think it's a done deal yet, but I believe we're close to having something finalized.
- bokchoi 15y ago> it's mostly strange that nobody else has it It looks like it is the same as clojure's doto macro: http://clojuredocs.org/clojure_core/clojure.core/doto http://clojuredocs.org/clojure_core/clojure.core/doto And it is strange more languages don't have it.
- edanm 15y agoI love the first answer to that question. And not just because I wrote it :) A little bit of C trivia: 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.
- djcb 15y agosimilarly, the 'approaches' pseudo-operator: while (i --> 0) { /* do something */ }
- repsilat 15y agoFor some variety, there's also the left-handed "goes to" operator. while (5 <-- x) {} //omits the zero and the much easier to type while (x --- 0) {} //equivalent to the "-->" operator
- cjensen 15y agoTo be clear, it works because X[Y] is a synonym for * (X + Y) Since addition is commutative, * (X + Y) can be rewritten as * (Y + X) which is a synonym for... Y[X]
- kghose 15y agoI have to say, this is what I come to HN for. I learned a lot from that link and doing wikipedia searches kept me entertained during lunch.
- weinzierl 15y agoSimilar, but for more exotic languages: "What is the worst programming language you ever worked with?" http://stackoverflow.com/questions/961942/what-is-the-worst-programming-language-you-ever-worked-with http://stackoverflow.com/questions/961942/what-is-the-worst-...
- nazar 15y agoWhat about strangest programmers needs :D : http://stackoverflow.com/questions/8525199/how-to-store-javascript-code-without-it-executing http://stackoverflow.com/questions/8525199/how-to-store-java... Edit: I think the title of the question was edited, the original was something like : "How to store javascript code in jquery variable without it executing?"
- j_baker 15y agoThe fact that this question is closed is one of the things that annoys me the most about stackoverflow. It sometimes seems like people can't ask interesting questions without others getting offended.
- philwelch 15y agoDeletionists.
- robryan 15y agoSeems everything that isn't a direct question/answer gets closed. Seems like they miss an opportunity with the discussion questions. They have really smart community that can't really converse on the site of something doesn't fit into he narrow question and answer definition.
- andrewflnr 15y agoThat's the whole point of StackOverflow. It's not a discussion platform. It's a Q&A platform. You can dispute whether that's a good idea, but they know exactly what they are doing.
- Flimm 15y agoFunnily enough, this is what I like most about stackoverflow. They know what they're good at, they know what use-cases the site is designed for, and they actively fight mission creep.
- halo 15y agoI find the amount of 'closed' questions on StackOverflow rather frustrating. I don't see how artificially restricting questions like this helps the site. That said, I find the responses rather unsatisfying. Language design errors and quirks rather than unusual but useful features.
- smackfu 15y agoThey just decided they didn't want to a polling site. I think the programmers stack exchange is where these questions go now, but who can tell? Generally I just post my questions somewhere obviously wrong and wait for someone to tell me where they go.
- nandemo 15y agoCounterpoint: I find it refreshing. In my opinion many otherwise interesting virtual communities degrade due to lack of some form of moderation. I don't see it as artificially restricting questions. They're enforcing the rules that preserve the character and quality of the site. It's not arbitrary: > This question is not a good fit to our Q&A format. We expect answers to generally involve facts, references, or specific expertise; this question will likely solicit opinion, debate, arguments, polling, or extended discussion. There are other places for that sort of free-form discussion. Separating them from the factual Q&A is a win-win.
- dustingetz 15y agoi bet it does a woozey on their databases.
- makmanalp 15y agoNo COME FROM? http://en.wikipedia.org/wiki/COMEFROM http://en.wikipedia.org/wiki/COMEFROM http://c2.com/cgi/wiki?ComeFrom http://c2.com/cgi/wiki?ComeFrom
- icebraining 15y agoIt's in the middle of the first page: http://stackoverflow.com/a/1995275/221786 http://stackoverflow.com/a/1995275/221786
- aerique 15y agoAh yes, all the Perl special variables! I actually did get scoffed at at work for using "use english" (while taking care it didn't affect regexps).
- cousin_it 15y agoFor more JavaScript awesomeness on par with what's already presented, see http://wtfjs.com/ http://wtfjs.com/ =)
- tm123 15y agoIdentifiers with blanks: http://tibleiz.net/zinc/ http://tibleiz.net/zinc/ (not that strange, but not so common)
- krzysz00 15y agoCommon Lisp has a global variable called read-base ,which controls also base numbers are read in with. This, IIRC, was put in because some earlier Lisps read numbers in octal, while others used decimal. Setting this variable to something higher than 10 can give you strange results. For example, nil => NIL (eql '() nil) => T (setf *read-base* 36) nil => 30477 (eql '() nil) ERROR: 19101 is not a function name; try using a symbol instead (|EQL| '() nil) => NIL For you non-lispers, the empty list '() is equal to the "false" value, nil. Also, the pipes are special syntax to allow for symbols which normally not parse as symbols. (The uppercase is for historical reasons too). The moral of this story: "See that control variable over there, the one that does weird things to parsing? Don't touch it unless you know what you're doing."
- RodgerTheGreat 15y agoI presume that CL standardized on a base as a default, though? Otherwise you have an appropriately recursive problem that every base is "base-10" in that base. edit: (setf read-base (+ 1 1 1 1 1 1 1 1 1 1)) :)
- krzysz00 15y agoI just went and checked, and the standard gives a default value of "10." for read-base , so decimal by default (I think). It also says: "Notes: Altering the input radix can be useful when reading data files in special formats. "
- renownedmedia 15y agoPHP: Personal favorite I used to quiz my students on: $a = "5 Monkeys"; $b = "2 Bananas"; var_dump($a); string(9) "5 Monkeys" var_dump($a + $b); int(7)