Welcome! Please see the About page for a little more info on how this works.

0 votes
ago in Syntax and reader by

Hello,

I'm surprised by the result of this code :

(if-let [{errors :errors} {}]
    errors
    true) => nil

I would have expected the result to be 'true'.

As indicated by the result, 'errors' evaluates to nil, but still it's the 'then' arm of the 'if' that is evaluated.

The guide on destructuring state that "You can utilize destructuring anywhere that there is an explicit or implicit let binding.", so I'm a bit puzzled.

Please, enlighten me. Thanks !

ago by
I've just have the idea to macro expand the code.

I can see that what 'if' evaluates is the empty map, not the result of getting the :error key.

Which explain the behaviour. Sorry to have bothered you for nothing
ago by
To add a bit to that - you can use any kind of destructuring to set any number of bindings, but the resulting `if` must get exactly one value. It's quite convenient to be able to express "compute this and if it's not a logical false, destructure like this and proceed" with just a single symbol.
ago by
Thank you for the comment. I guess what confused me in the first place is that I got the steps in the wrong order : I thought it was compute/destructure/evaluate truthiness, whereas  as you said it's compute/evaluate truthiness/destructure.

Please log in or register to answer this question.

...