-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Do notation changes #12117
Do notation changes #12117
Conversation
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.
It seems to me like we need to rethink the do notation from the ground up. This notation doesn't feel like it fits the language, we've already decided that it will be removed from closures and for templates we should really create a different keyword or come up with an alternative way to pass more than one body.
This change seems like a small thing that tries to fix some flaws in the do notation, and it just makes it more prevalent and more annoying to use. Why should templates have to strip the do nodes?
Maybe, but the do notation is part of Nim for quite some time now.
Well, as an ugly syntax to pass a closure as the last argument of a proc, it doesn't really cause problems.
I use this pattern, works like a charm: myMacro:
body1:
echo 1
echo 2
body2:
echo 3
echo 4
This is not just about Templates should not be written with the do notation in mind. The do notation is just an ugly syntax for lambda expressions. For historical reasons the do notation has been used for some templates, because it was the best option at the time. I found a fix to let them keep their syntax, but I would not recommend to design templates like this anymore. Nim does not need for every corner case a feature that makes it look super nice. |
Yes, but it feels like such a bolted on feature to me that I think it's worth it to deprecate it and provide a good alternative.
This works well, but writing a macro for simple templates can be a PITA. Maybe if we could create a macro to make it simpler or at least some code examples that can be readily copy and pasted we could give people a smooth transition?
Indeed, but it breaks templates that use it. Since you're already making this breaking change, why not take it a step further and just deprecate the do notation while you're at it? :) |
Regarding your example myMacro:
body1:
echo 1
echo 2
body2:
echo 3
echo 4 does that match with macro myMacro(body1, body2: untyped) because if so, I think that sufficiently ergonomic to replace the do notation altogether. I agree with @dom96, the do notation feels foreign, especially the absence of indentation for it (like case of actually). If we have a good alternative we should remove it. With the changes, I'm concerned about backward compat (#12114 (comment)) |
Yea maybe.
Well because I think the ugly sortable.sort do (a, b: int) -> int:
result = cmp( abs(base - a), abs(base - b) )
# Yea, not really multiline, but I don't have a better example right now. |
@mratsim I am afraind, no it is not that simple. This is more complete: macro myMacro(metabody: untyped) =
var body1, body2: NimNode
for body in metabody:
body.expectKind nnkCall
if body[0].eqIdent "body1":
body1 = body[1]
elif body[0].eqIdent "body2":
body2 = body[1]
else:
error("unexpected section", body)
# [...] do stuff with body1 and body2
echo body1.treeRepr
echo body2.treeRepr
myMacro:
body1:
echo 1
echo 2
body2:
echo 3
echo 4 If this is really a demanded common pattern, I could write a helper macro for this. |
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
since the tests are passing, this PR is ready to be merged. |
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
Unfortunately it is a common pattern to pass two blocks of code to a template with the do notation. These templates are now broken. To fix them there is now a
stripDoNode
macros inmacros.nim
that can now manually be injected to strip thennkDo
node which was previously stripped by the parser.Since
nnkStmtList
is not automatically converted to a proc argument, users are now required to pass them as lambda expressions. We have three syntaxes to choose from.Note:
Don't merge it yet. This is a draft to see how many packages would break if this is changed.
Missing: