Skip to content
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

Open
matteyas opened this issue Aug 23, 2016 · 4 comments
Open

Comments

@matteyas
Copy link

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?

@NN---
Copy link
Member

NN--- commented Aug 24, 2016

It is possible but then you have to write $cond in while once again.

@matteyas
Copy link
Author

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 firstHit = true; inside the loop and setting firstHit to an immutable instead of a mutable.

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. 😃

@Ziaw
Copy link
Member

Ziaw commented Aug 25, 2016

@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)
]>
}

@ionoy
Copy link
Contributor

ionoy commented Aug 25, 2016

@matteyas $cond might have a mutating behavior, which would lead to very obscure bugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants