feat(stdlib): add if-let and while-let macros#1543
Conversation
|
Just realized that perhaps this probably should have gone into controlmacros.carp instead. Oopsies. Thoughts? |
8aad9c0 to
be35d05
Compare
|
I went ahead and moved them to Controlmacros.carp and also added when-let as well since we were here anyways |
| (cond-internal xs)) | ||
|
|
||
| (doc refstr "stringifies `x` and takes the reference of that string.") | ||
|
|
There was a problem hiding this comment.
Can we get rid of that empty line change to get the diff a bit cleaner?
| (assert-equal test | ||
| "found 10" | ||
| &(if-let [x (Maybe.Just 10)] | ||
| (fmt "found %d" x) | ||
| @"not found") | ||
| "if-let works with Just") | ||
| (assert-equal test | ||
| "not found" | ||
| &(if-let [x (the (Maybe Int) (Maybe.Nothing))] | ||
| (fmt "found %d" x) | ||
| @"not found") | ||
| "if-let works with Nothing") | ||
| (let-do [counter 0 | ||
| res []] | ||
| (while-let [x (if (< counter 3) (Maybe.Just counter) (the (Maybe Int) (Maybe.Nothing)))] | ||
| (do | ||
| (set! res (Array.push-back res x)) | ||
| (set! counter (inc counter)))) | ||
| (assert-equal test | ||
| &[0 1 2] | ||
| &res | ||
| "while-let works as expected")) |
There was a problem hiding this comment.
I think this can be removed since you moved the tests also?
| (cons 'do (expand (apply ignore* forms)))) | ||
|
|
||
| (doc if-let "Conditional execution based on a `Maybe` value. | ||
| If `expr` evaluates to `(Just x)`, the `then` branch is executed with `var` bound to `x`. |
There was a problem hiding this comment.
The user can’t see expr and var, so this explanation might be confusing.
| (Maybe.Nothing) %else))) | ||
|
|
||
| (doc while-let "Looping execution based on a `Maybe` value. | ||
| Repeatedly evaluates `expr`. As long as it returns `(Just x)`, the `body` is executed with `var` bound to `x`.") |
| (Maybe.Nothing) (break))))) | ||
|
|
||
| (doc when-let "Conditional execution based on a `Maybe` value. | ||
| If `expr` evaluates to `(Just x)`, the `body` is executed with `var` bound to `x`.") |
| (assert-equal test | ||
| "not found" | ||
| &(if-let [x (the (Maybe Int) (Maybe.Nothing))] | ||
| (fmt "found %d" x) | ||
| @"not found") | ||
| "if-let works with Nothing") |
There was a problem hiding this comment.
I was wrong! This one needs to move over.
hellerve
left a comment
There was a problem hiding this comment.
Two of my comments are seemingly still unaddressed.
| ;; type safety. In Carp, a single match block cannot handle both Maybe.Just | ||
| ;; and Result.Success constructors simultaneously. |
There was a problem hiding this comment.
"a single match block cannot handle both Maybe.Just and Result.Success constructors simultaneously" is tautological, since they're different types, no?
- Added if-let for conditional execution on Maybe values. - Added while-let for looping execution on Maybe values. - Added when-let for simpler conditional unwrapping. - Moved these macros to core/ControlMacros.carp for better organization. - Integrated comprehensive tests into test/control_macros.carp.
…and add Result counterparts - Enhanced if-let, when-let, and while-let to support multiple bindings for Maybe types.\n- Added if-let-ok, when-let-ok, and while-let-ok for Result types with multiple binding support.\n- Improved macro implementation using internal dynamic functions for robust scoping.\n- Added comprehensive test cases in test/control_macros.carp.
195aafe to
22dfdc9
Compare
22dfdc9 to
8c84e2e
Compare
|
oh god guys git is hard and I made a very ugly commit history by accident. sorry. unsure how to make it prettier. |
that's fine, it'll get squashed |
|
It looks like a bunch of my comments are still relevant here. |
Hey! I noticed that Carp was missing some basic Lisp staples for handling
Maybevalues more easily, so I've implementedif-letandwhile-let.These macros make it much cleaner to unwrap an optional value without needing a full
matchblock every time.What's new:
MaybeisJust.Maybe.test/macros.carpto make sure they're solid.Disclaimer: This PR was built with the assistance of Gemini (an AI assistant) working in collaboration with @sqrew to improve the Carp standard library ergonomics.