
Strangest Language Features - antoviaque
http://stackoverflow.com/questions/1995113/strangest-language-feature
======
masklinn
Smalltalk'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).

~~~
bokchoi
> 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>

And it is strange more languages don't have it.

~~~
masklinn
It is pretty nice (and makes Clojure jump up a place or two in the completely
unranked list of "languages I really need to learn"). It looks very similar
yes, although there's a difference in that `doto` can only returns its `this`.

~~~
ajuc
Clojure has something even cooler than doto. It's called "thrush operators":

<http://clojuredocs.org/clojure_core/clojure.core/-%3E> and
<http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E>

------
bitwize
COBOL'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
I 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>) 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)

------
halo
I 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.

~~~
nandemo
Counterpoint: 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.

------
j_baker
The 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.

~~~
robryan
Seems 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
That'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.

------
krzysz00
Common 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
I 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
I 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. "

------
darklajid
Second SO repost. Previously discussed with > 80 comments here:

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

~~~
danso
I 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/>

The other has a slug in it:
[http://stackoverflow.com/questions/1995113/strangest-
languag...](http://stackoverflow.com/questions/1995113/strangest-language-
feature)

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
Nope, 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.

------
makmanalp
No COME FROM?

<http://en.wikipedia.org/wiki/COMEFROM> <http://c2.com/cgi/wiki?ComeFrom>

~~~
icebraining
It's in the middle of the first page:
<http://stackoverflow.com/a/1995275/221786>

------
edanm
I 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
similarly, the 'approaches' pseudo-operator:

while (i --> 0) { /* do something */ }

~~~
repsilat
For 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

------
cousin_it
For more JavaScript awesomeness on par with what's already presented, see
<http://wtfjs.com/> =)

------
kghose
I 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.

------
nazar
What about strangest programmers needs :D :
[http://stackoverflow.com/questions/8525199/how-to-store-
java...](http://stackoverflow.com/questions/8525199/how-to-store-javascript-
code-without-it-executing)

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?"

------
xd
PHP; 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
> only except strings

A strange feature of the English language is that this doesn't work; you have
to use 'accept' here.

------
weinzierl
Similar, but for more exotic languages: "What is the worst programming
language you ever worked with?"
[http://stackoverflow.com/questions/961942/what-is-the-
worst-...](http://stackoverflow.com/questions/961942/what-is-the-worst-
programming-language-you-ever-worked-with)

------
protomyth
Icon's conditional statements were pretty odd
<http://en.wikipedia.org/wiki/Icon_(programming_language)>

------
ww520
The Multi-Dimensional Analog Literals template in C++ is amazing.

------
aerique
Ah 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).

------
tm123
Identifiers with blanks:

<http://tibleiz.net/zinc/>

(not that strange, but not so common)

------
danso
Wow...not a single SO response that said Ruby? I guess that's a good thing for
us Rubyists?

~~~
cobrausn
Actually, there is, just not as many upvotes as the others.

<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
I 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.

------
renownedmedia
PHP: 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)

------
atlbeer
This 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
Octal notation, with an invalid number. Makes perfect sense.

It's not a bug, behaves like specified:

[https://developer.mozilla.org/en/JavaScript/Reference/Global...](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt)

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._

~~~
groby_b
"Behaves as specified", "makes perfect sense", and "not a bug" are three
entirely different concepts.

It does _not_ make perfect sense to anybody who's never used octal. That is
presumably the vast majority of anybody born past, say, 1980.

As such, it _is_ a bug in the sense that it violates expectations. It's just a
bug in the spec, not in the code.

~~~
gcp
Numeric constants starting with 0 define octal values in many languages, hell
they do so even in Java. Does that mean all those languages are buggy? I'd say
you're going to find features that don't make sense to anyone who's never used
them...pretty much anywhere.

I would think the issue is more the behavior of "parse as much as you can
until you can't, and return what you got so far", which is what causes the
unexpected result of returning 0 instead of bailing out. However _that_
apparently wasn't fixed in ECMAScript 5, presumably for backwards
compatibility.

~~~
groby_b
I'd say that the entire concept of octal literals is a bug, yes. (It's almost
_entirely_ useless, a leftover from the days of yore. When for Christmas, we
got naked, wrapped ourselves in punch tape versions of the latest "editor",
and danced around the Christmas tree while setting the line printer on fire.
But I digress.. ;)

But fine, if I at least got "undefined" (well, null) as a result of parsing an
invalid octal string, it'd be not quite as bad. So yes, if we talk root
causes, you put your finger on that. The anachronism of octal parsing just
exposes it.

------
niyazpk
In JavaScript:

    
    
        (NaN === NaN) is false
        (NaN !== NaN) is true

~~~
raganwald
This 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-...](https://github.com/raganwald/homoiconic/blob/master/2009-02-02/hopeless_egocentricity.md)

~~~
masklinn
> 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`.

