4 ms·
I also don't feel that aif has much value, and doesn't resemble the linguistic anaphora that it is inspired by. That's why I did a "one up" in my ifa design, t
by kazinator 10y ago
I also don't feel that aif has much value, and doesn't resemble the linguistic anaphora that it is inspired by.
That's why I did a "one up" in my ifa design, to outdo the silly aif macro.
In English, I can say, "if x exceeds 3, then print it". When I say that, it's obvious to everyone that by "it" I do not mean the Boolean value of "x exceeds 3". I mean x. So aif is nothing like the anaphoric "it" we encounter in English.
I tried to make ifa at least superficially try to capture some of that: (ifa (> (whatever) 3) (print it)): prints value of (whatever). Also, suppose (whatever) is a place. We want to be able to say "If (whatever) exceeds 3, increment it": (ifa (> (whatever) 3) (inc it)). And we don't want (whatever) evaluated twice.
The expression (aif whatever expr-containing-it) doesn't buy us much over (iflet (var whatever) expr-containing-var). Both communicate an organization of code around something returned from whatever, which can be nil (don't run the code) or some non-nil object (do run the code, refer to the thing as var). To me also, aif does look mostly like keystroke saving.
My iflet/whenlet/whilet/condlet constructs bind multiple variables. The last one is tested: (iflet ((x whatever) (y another thing)) expr-evaled-if-y-true). This makes the macros helpful in more situations. (Next release will make the y optional if expr doesn't need it.)
E.g.:
(whenlet ((x (get-something arg))
(y (get-some-dependent-thing x other-arg (calculation)))
;; code run if y is true, with access to x too
)
Or:
(whilet ((next (get-next-object stream))
(maybe-foo (if next (get-foo-property next))))
;; process only maybe-foo things from stream
)
We should try towrite macros that have some half decent level of impact: bang for the parenthesis and symbol. Especially if they are intended for enduring public use.
- groovy2shoes 10y agoVery neat!