Open roryokane opened 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.
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]
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 :)
If you would like to send a pull request with suggested updates that would be great!
I was writing some Wisp code and needed to write an equivalent to this JS:
I had a very hard time figuring out what Wisp would do that. I tried forms like these:
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:
The README does not make that clear. This is all it says about
get
: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:But not if the name is in a variable:
get
is the solution here:So this use, too, should be described in words and used in a code example.