Currently in Cab, there are only 3 keywords that aren't defined as infix/postfix operators: if
, then
and else
. (and and
, or
which are infix, not
which is a prefix opetator, just like -
)
I believe a this doesn't fit the language because there are no other "special cased" keywords. I want to change their behavior, also removing the if
keyword in the process of redesigning this approach. The new syntax for "if else" will be:
cond then y else n
The then
operator works like this: cond then y
cond
is false, it evaluates to undefined
, if it isn't, it simply evaluates to y
The else
operator works like this: maybeUndefined else n
maybeUndefined
is undefined, it simply evaluates to n
. If not, it evaluates to maybeUndefined
then
has higher binding power, so you can chain then
and else
.
Another benefit of this is that you will be able to do setThatDoesNotHaveFoo.foo else "foo didn't exist"
without special casing (like the or
in Nix), as .foo
is undefined.
Thoughts?
I think I'll keep the normal if else then right now. Have some doubts about precendence and the general ergonomics of it
But I'll definitely look into it later when more stuff is done
I was thinking about this a bit yesterday. FWIW, I think its a fairly nice idea. It's basically the "ternary" operation from other languages at that point, which I am fond of.
It is exactly like ? and :, but each operator is seperate
However this might require you to put () everywhere, which I'm not fond of. I'll look at some snippets inside nixpkgs lib and rewrite them to use this, seeing how it works
Last updated: Oct 12 2024 at 21:29 UTC