Olical / aniseed

Neovim configuration and plugins in Fennel (Lisp compiled to Lua)
https://discord.gg/wXAMr8F
The Unlicense
606 stars 28 forks source link

Add when-let as a core macro #124

Closed NoahTheDuke closed 2 years ago

NoahTheDuke commented 2 years ago

I noticed a lot of (let [...] (when ... in conjure, so it seemed like a good idea to add this trusty helper macro to Aniseed!

I originally had it as single-pair Clojure version but given fennel's if being variadic, seemed appropriate to make this one variadic as well. I originally had an assertion that the bindings is sequential? as seen in the comment, but I couldn't figure out how to make fennel see that recursive calls were passing in sequences and not tables. icollect didn't help, and neither did [(unpack r)], so I'm not sure what it would take.

Olical commented 2 years ago

Cool idea! However, isn't this more "clever" than Clojure's when-let? 🤔 as I recall Clojure's *-let macros only let you define one binding, yours allows many? Which is maybe okay but the more complexity makes me fear documenting and supporting it. Maybe I'm being silly, but I quite liked the simple rules of "it only lets you do one thing, but it saves you doing (when (let))".

What do you think? Am I wrong and are you copying an implementation that DOES allow many? If so, is it an implicit (and ...) around the values? Do you ever use that feature? Would you miss it if we simplified it to one binding (which I think is easier to implement / get right / read in the future?)?

Just want to avoid complexity anywhere I can if possible, especially in macros 😄

NoahTheDuke commented 2 years ago

You're right, hah, which is why I said "I originally had it as single-pair Clojure version but given fennel's if being variadic, seemed appropriate to make this one variadic as well." in the second paragraph.

I think both are fine. I've occasionally found myself reaching for a when-let* but much less frequently than the single-pair version. Up to you, really!

Olical commented 2 years ago

Ah sorry! I should've read more! I'm still ill and feeling a little fuzzy so I blame it on that 😬 heading out for a bit but will take a look when I'm home. I'm on the fence, sometimes I've wanted many bindings in when-lets but something is drawing me to having it as simple as possible because macros can be scary (and produce more Lua!) and being totally in line with Clojure is appealing to me.

But I'll have a think as I walk! I do like the idea though, I'll probably add if-let too... hopefully it doesn't clash with anyone else's stuff. I guess if someone has defined their own when-let it'll take precedence over this one anyway.

NoahTheDuke commented 2 years ago

Oh yeah, I guess there isn't an if-let. I could add that here if you want it too.

As for the variadic, it really depends on whether you want to match Clojure or Fennel, I think.