Closed bbrk24 closed 1 month ago
Supersedes #756 I suppose
A couple thoughts:
I'm reluctant to add any more reserved words like guard
. Would unless x? := getX() else return
be an acceptable spelling of this feature?
Maybe upgrading pipes to better handle piping to return
/throw
and allowing something similar to declaration conditions could help with this. https://github.com/DanielXMoore/Civet/issues/897#issuecomment-2002531328
x := getX() !> return
---
let ref = getX(); if(!ref) return ref; const x = ref;
Possible conditional pipe options: ?>
, !>
, ?|>
, !|>
maybe something else?
unless-else reads like a double-negative, but otherwise I'd be fine with it.
Pipe conditions do seem like a reasonable possible approach. We might need one for truthy and one for nonnull. We've talked about ?|>
before to act like "if nonnull, then pipe into rhs".
Given how much guard
resembles unless
, I'd be tempted to spell it like so:
unless x? := getX()
throw new Error "Missing x coordinate"
unless y? := getY() then return
unless z? := getZ() then return
setPoint x, y, z
unless x := ...
isn't very helpful; it just exposes a falsey value for x
inside the block. The proposed alternative would be to expose the declaration outside the unless
block instead — essentially opposite of if
, which kind of makes sense?Alternatively, we could pull the declaration outside:
x := getX() ?? throw new Error "Missing x coordinate"
y := getY() ?? return
z := getZ() ?? return
setPoint x, y, z
This works for throw
, but not for return
, because we don't yet have a way to expressionize return
. When it's at the top level of a declaration like this, I imagine we could handle it though.
Now that we have
isExit
for other reasons, Swift-styleguard
seems implementable. At first glance, it seems identical tounless
:However, there are two key differences:
guard x? := getX() else { ... }
), the bindings are only available outside theelse
block.STATEMENTS
must be an "exit" of some sort, in order for1
to be possible.The primary use case is to prevent "pyramids of doom" when there are many bindings to be done: