Mercerenies / gdlisp

Lisp on the Godot platform
GNU General Public License v3.0
146 stars 1 forks source link

Compound Assignments #18

Closed Mercerenies closed 2 years ago

Mercerenies commented 3 years ago

GDScript, of course, has +=, -=, etc. compound assignment operators, which are far more convenient than repeating the name of the thing being assigned to.

There is some precedent here. Common Lisp uses incf for += and decf for -=. We could always define several names, possibly as part of a more general setf interface a la Common Lisp.

Mercerenies commented 2 years ago

Following in footsteps somewhat close to Clojure, we've designed a macro called update. The following are equivalent

(update xyz (+ 1))
(set xyz (+ xyz 1))

and, for syntactic convenience, you are also permitted to use a symbol for unary functions, so the following two are equivalent

(update xyz foobar)
(set xyz (foobar xyz))

Note that this does work with all set forms, since it's merely a syntactic macro. However, if there are side effects in the xyz part, they may be evaluated multiple times (current implementation will always evaluate them twice, but I do not guarantee that).