Strangest Language Features
stackoverflow.com
stackoverflow.com
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).
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.
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.
http://clojuredocs.org/clojure_core/clojure.core/-%3E and http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E
c := LeftAsAnExerciseForTheReader();
with c do
begin
Add(3);
Add(4);
Add(5);
Add(6);
end;Now I have a name, the cascades operator.
I don't think the syntax is especially elegant/readable, though.
list := OrderedCollection new
add: 1;
add: 2;
add: 3;
yourself
There's actually a literal syntax for Arrays, but the above example applies to all code and is used heavily in constructors and anything using a builder pattern.Much in the same way as Java, smalltalk-80's arrays are not the most useful thing (they're significantly worse than Java's if I remember correctly, as you can only put other literals in them). So most collections in "user" code are not going to be arrays.
I believe gnu smalltalk extends arrays to be more useful (and adds other collection literals).
What? That's not even close to true. You are misunderstanding something. Actually, I see I think the issue... most Smalltalks have progressed a bit since Smalltalk 80 and now have a fully functional array literal syntax whereas you're talking about the very restrictive original syntax from decades ago.
I'm talking about Smalltalk-80, hence specifying it, but I know Dolphin Smalltalk still has that restriction.
In Java this is:
List<Integer> c = new ArrayList<>() {{ add(3); add(4); add(5); add(6); }};
1. Because it's more limited in that it can only initialize an object and it requires that the type being initialized be non-final
2. Because it's kind-of a hack, using instance initializers (itself not a well-known feature) and anonymous classes (not overly common for concrete classes in java) simultanously
I also like to think people can be delighted in looking for treasures after a hint, and it's not too hard to find references to that trick, whereas references to Smalltalk's cascading operators are sparse and lost in the noise.
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!
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)
That said, I find the responses rather unsatisfying. Language design errors and quirks rather than unusual but useful features.
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.
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."
edit: (setf read-base (+ 1 1 1 1 1 1 1 1 1 1)) :)
"Notes:
Altering the input radix can be useful when reading data files in special formats. "
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...
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...)
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.
while (i --> 0) { /* do something */ }
while (5 <-- x) {} //omits the zero
and the much easier to type while (x --- 0) {} //equivalent to the "-->" operatorSince addition is commutative, * (X + Y) can be rewritten as * (Y + X) which is a synonym for... Y[X]
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?"
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;
A strange feature of the English language is that this doesn't work; you have to use 'accept' here.
(not that strange, but not so common)
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
...
endFolks 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.
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 # Define two class variables in two different classes:
class MyClass
@@base = 1
end
class OtherClass
@@base = 2
end
# Define it in the superclass too:
class Object
@@base = 3
end
class MyClass
# Magic! The subclass-variable has now changed!
p @@base # => 3
end
class OtherClass
# In both classes in fact:
p @@base # => 3
# And finally, let's reset this:
@@base = 4
end
class Object
# Whoops, this changes the superclass too:
p @@base # => 4
end
Explanation: Two class variables with the same name can't exists in the
same class hierachy. Therefore, class variable lookup (and setter) will
always traverse to the upmost-class where the variable is defined.(Hope you don't mind that I wrote these in two different posts.)
$a = "5 Monkeys";
$b = "2 Bananas";
var_dump($a);
string(9) "5 Monkeys"
var_dump($a + $b);
int(7)
parseInt('06') // 6
parseInt('07') // 7
parseInt('08') // 0
parseInt('09') // 0
parseInt('10') // 10
parseInt should really return NaN for '08', '09' etc
It's not a bug, behaves like specified:
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.
Also, that document says the behavior is nonstandard, so I don't even see how you can say it's behaving as specified.
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".
It's not. That isn't what happens, either. The parser stops when it sees the 9.
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.
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.
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.
For a parser to ignore trailing garbage after it has parsed as far as it can is usually a sign of a poor-quality parser. On topic: an odd feature of Pascal is that all text following the final 'end' '.' in the file is ignored. But good quality compilers still issue a warning.
User enters 08 for August, even returning NaN would be a mistake.
parseInt('08', 10) // => 8
This tells it explicitly that this is a base-10 number, rather than letting it guess at the base. You can also use this to decode things in base n, n <= 36.Much worse than this one obviously, caused if I recall by not checking how strtol set endptr.
(NaN === NaN) is false
(NaN !== NaN) is trueIf 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-...
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`.