wisp-lang / wisp

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

Document the missing uses of `get` in README #65

Open roryokane opened 11 years ago

roryokane commented 11 years ago

I was writing some Wisp code and needed to write an equivalent to this JS:

color[3] = someCalc();

I had a very hard time figuring out what Wisp would do that. I tried forms like these:

(set! color[3] (someCalc))
(set! (.-3 color) (someCalc))
(set! (.3 color) (someCalc))
(set! (:3 color) (someCalc))

But none of them were equivalent. Finally, after a lot of guessing and skimming through the Clojure data structure docs, I found that the answer was this:

(set! (get color 3) (someCalc))

The README does not make that clear. This is all it says about get:

Compound properties can be access via get special form

(get templates (.-id element))

It should the mention the search-friendly key words “array index”, and maybe also “square bracket access”. It should also include a code example of using get to access an index of an array.

get has another use that should be documented, too. It is the way to get a property of an object literal when the name of the property is in a variable. This works if you know the property name:

(:name person)

But not if the name is in a variable:

(def good-prop :name)
(good-prop person)

get is the solution here:

(def good-prop :name)
(get person good-prop)

So this use, too, should be described in words and used in a code example.

Gozala commented 11 years ago

I was writing some Wisp code and needed to write an equivalent to this JS:

color[3] = someCalc();

I had a very hard time figuring out what Wisp would do that. I tried forms like these:

That's a fair point! I'm to keep wisp syntax clojure(script) compatible and there for support only forms supported by it, even though I hate it's syntax for accessing computed properties x[y]

(set! color3)

I think that expression would be too ambiguous for clojure and go against polish notation of clojure.

(set! (.-3 color) (someCalc))

I would happily made it work of clojure supported it, but I guess at least compiler can be improved to produce errors.

(set! (.3 color) (someCalc))

That's method invocation

(set! (:3 color) (someCalc))

This actually works in latest version and produces:

(color || 0)["3"] = someCalc()

Although it seems that now clojure(script) finally advertizes:

(aset object field value) ;; => object[field] = value

So I may just add aset macro and suggest same in docs.

Gozala commented 11 years ago

It should the mention the search-friendly key words “array index”, and maybe also “square bracket access”. It should also include a code example of using get to access an index of an array.

Note that (get x y) isn't equivalent of x[y] as it compiles to (x || 0)[y] which is just shorter way to of doing x ? x[y] : undefined

For computed property access one can use (aget x y) that compiles to x[y]

Gozala commented 11 years ago

get has another use that should be documented, too. It is the way to get a property of an object literal when the name of the property is in a variable. This works if you know the property name:

(:name person)

That kind of feature I'm not yet decided upon. In clojure :foo is different from "foo" so for following is true for clojure:

(:x {:x 1})    ;; => 1
(:x {"x" 1))   ;; => nil

Later in wisp will also return 1. I'm still not sure weather it's a good idea, that's why it's not documented :)

Gozala commented 11 years ago

If you would like to send a pull request with suggested updates that would be great!