-
Notifications
You must be signed in to change notification settings - Fork 89
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
Question about implementation in source (disclaimer: not really an issue) #13316
Comments
It is possible but then you have to write $cond in while once again. |
macro WhileOtherwise(cond, body, otherwiseBody)
syntax ("while", "(", cond, ")", body, "otherwise", otherwiseBody)
{
<[
def firstHit = $cond;
while ($cond)
{
$body
}
when (!firstHit)
$otherwiseBody;
]>
} Is that valid? The only changes is taking away Do you mean that using $cond twice is less efficient? That can't be the case; $cond will be evaluated on each loop anyway. I am certainly open for other perspectives, but please explain. 😃 |
@matteyas $cond will be evaluated at least twice macro without mutable might look like this: macro WhileOtherwise(cond, body, otherwiseBody)
syntax ("while", "(", cond, ")", body, "otherwise", otherwiseBody)
{
<[
def loop(firstHit)
{
match ($cond, firstHit)
{
| false, true => $otherwiseBody;
| false, false => (); // end loop
| true, _ => $body; loop(false);
}
}
loop(true)
]>
} |
@matteyas |
I noticed a very weird implementation in here: https://github.com/rsdn/nemerle/blob/master/macros/core.n
Look at the WhileOtherwise macro. First of all, it defines firstHit as a mutable. Secondly, it sets firstHit = true all the time (this will probably be optimized by the compiler, but still seems unnecessary).
Wouldn't it be possible to just use def firstHit = $cond instead? No need for a mutable variable, no need to set it in the loop. Maybe the language doesn't work that way?
The text was updated successfully, but these errors were encountered: