wisp-lang / wisp

A little Clojure-like LISP in JavaScript
https://gozala.github.io/wisp/
Other
982 stars 68 forks source link

dash to camelcase not working as expected for object keys #137

Closed vshesh closed 8 years ago

vshesh commented 8 years ago

If I write a function like

(defn f [x] x.y-z)

it will compile to

var f = function f(x) {return x.yZ}

However, if I try to call this function with

(f {:y-z 1})

That compiles to

f({"y-z": 1})

which is obviously not symmetrical, and returns undefined instead of 1.

I'm curious as to whether this behavior is expected, and if so, why, or whether this is unintended (and if so, how we might fix it). It's just strange to have to have pass f({:yZ 1}) to a function that uses :y-z instead.

robjens commented 8 years ago

Yes the behavior is as intended. It is not only dashes - to underscores _ but far more and on (or at least what people would expect) infinitely deep objects. The performance hit would be simply too much. Keep in mind, unlike Clojure, JavaScript does allow self-reference and circular dependencies. The translation of those keys would be a nightmare, and then one would still have to translate any code using their names as well e.g. (:foo? bar) would need to become (:isFoo bar). The number of symbols is also extensive:

And all sorts of combinations possible. If you take a peek at the wisp backend, you'll see that for symbols alone, this already quickly becomes messy. The way it is now, keywords are just strings and that is probably for the best. To treat them as symbols or functions would be stretching it and definitely isn't currently supported.

vshesh commented 8 years ago

Ok, that makes sense. I suppose the thing to do is use (get x :y-z) which is the correct clojure way to access an element anyway. the . syntax is supposed to be for interop, so the fact that that compiles to camelCase to match js objects is actually the right thing. Thanks for the explanation!