mirror of
https://github.com/janet-lang/janet
synced 2026-10-20 06:22:27 +00:00
The `if-let` construct - together with the fact that only `nil` and `false` are falsey - is very handy to express monadic chains over maybe/optional values:
```janet
(if-let [resp (make-http-request) # is nil on failure
{:status stat :body body} resp # always truthy, binds `stat` and `body`
check (= 200 stat) # continue only on 200 OK
json (json/decode body true) # is nil on failure
{:foo foo} json]
foo)
```
However, none of the bindings are available in the false branch, which
could be helpful to have for error reporting or fallback logic:
```janet
(if-let [resp (make-http-request)
{:status stat :body body} resp
check (= 200 stat)
json (json/decode body true)
{:foo foo} json]
foo
(if (= 401 stat) # unauthorized, refresh the token
(try-again)
nil))
```
This PR makes all symbols introduced by the bindings of an `if-let` available in all branches.