-
Notifications
You must be signed in to change notification settings - Fork 119
feat: Null coalesce operator #5722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
src/mo_frontend/parser.mly
Outdated
| { t } | ||
| | QUEST t=typ_un | ||
| { OptT(t) @! at $sloc } | ||
| | NULLCOALESCE t=typ_un |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having both QUEST and NULLCOALESCE tokens causes ?? to be lexed as NULLCOALESCE, not double QUEST.
Just adding missing parsing where double ?? is allowed.
This slightly changes the region, but that is a small issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could actually fix-up the inner sloc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we could set the correct region for the inner one, I'll change that if we decide on going forward with the ?? operator
src/mo_frontend/parser.mly
Outdated
| { AndE(e1, e2) @? at $sloc } | ||
| | e1=exp_bin(B) OR e2=exp_bin(ob) | ||
| { OrE(e1, e2) @? at $sloc } | ||
| | e1=exp_bin(B) NULLCOALESCE e2=exp_bin(ob) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a bit like a conditional, we could also consider allowing a block on the rhs (like an else branch).
| e1=exp_bin(B) NULLCOALESCE e2=exp_nest
Maybe a bad idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! Maybe allowing a block on the left? so that
do ? {
} ?? defaultWould parse properly without a parenthesis?
Not allowing a block on the RHS would parse records better yes? Without extra brackets
| Prim.debugPrint("Upgraded (default)"); | ||
| }; | ||
| }; | ||
| let canister = testCanister ?? Prim.trap("null canister"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FTR this and the examples below could all be written already using let-else
| let canister = testCanister ?? Prim.trap("null canister"); | |
| let ?canister = testCanister else Prim.trap("null canister"); |
|
Looks nice enough to me - lets see what the others think. Are there any other (commonly used) symbols for It would be nice if we could get notation that works for any binary sum... |
|
Parser hacks also have a human aspect. I’d call it ( OTOH @christoph-dfinity has a point in saying that it is a short-circuit operator and should have name like |
Since we try to help an AI write more concise code we should stick to |
|
TODO: Examples: dailyLogs.get(caller) ?? do {
dailyLogs.add(caller, Map.empty<Nat, DailyRecord>());
getDailyLogs(caller);
};RHS without block vs with block foo.bar(qux) ?? { x = 0; z = null };vs foo.bar(qux) ?? { { x = 0; z = null } };LHS with block: do ? {
...
} ?? defaultDoes it work? |
|
Just writing it up before I forget. Maybe we could disambiguate |
That's a good idea, I was not aware of the |
Right sorry, I should've linked it. It's a bit messy but the logic is here: motoko/src/mo_frontend/lexer.ml Lines 49 to 53 in f24d95b
and a bit more over here: motoko/src/mo_frontend/lexer.ml Lines 74 to 78 in f24d95b
|
Thanks! No worries I would have found it 😄 I meant that I was not aware of that mechanism when I first added the |
Closes #5144
Reduces verbosity of common expressions like
To just
opt ?? defaultOrTrap(...)The operator
??is commonly used in other mainstream languages like TS/JS and C# so it is familiar for both humans and AI.Follow up: @rvanasa @christoph-dfinity we would need to update the grammar for tools like the formatter, lintoko, vscode etc, yes?