Skip to content

Quoted var assignment #3878

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

Closed
nicolasstucki opened this issue Jan 20, 2018 · 5 comments
Closed

Quoted var assignment #3878

nicolasstucki opened this issue Jan 20, 2018 · 5 comments

Comments

@nicolasstucki
Copy link
Contributor

To be able to use var we need to extend Expr[T] with a VarExpr[T] that allows the expression to be used in the left-hand side of an expression.

def whileNotZero(body: VarExpr[Int] => Expr[Unit]) = '{
  var x = 0
  while (x == 0) ~body('(x))
}
whileNotZero(x => '{ ~x = ~x - 1 })

Currently in '{ ~x = ~x - 1 }, the left hand side cannot be spliced in. The VarExpr[T] would allow x to be spliced on the left hand side of the assignment.

@biboudis
Copy link
Contributor

@nicolasstucki Maybe with some guidance I could have a look into that.

@nicolasstucki
Copy link
Contributor Author

We should first merge #3883 or build on top of it. I am currently not sure how it should work exactly. I would have to experiment with it to be able to give some guidance.

@biboudis biboudis self-assigned this Feb 19, 2018
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Feb 27, 2018
@nicolasstucki
Copy link
Contributor Author

nicolasstucki commented Feb 27, 2018

An alternavie library implementation is possible

sealed abstract class VarRef[T] {
  def update(expr: Expr[T]): Expr[Unit]
  def expr: Expr[T]
}

object VarRef {
  def apply[T: Type, U](init: Expr[T])(body: VarRef[T] => Expr[U]): Expr[U] = '{
    var x = ~init
    ~body(
      new VarRef {
        def update(e: Expr[T]): Expr[Unit] = '{ x = ~e }
        def expr: Expr[T] = '(x)
      }
    )
  }

}

object Test {
  VarRef(4)(varRef => '{ ~varRef.update(3); ~varRef.expr })
}

But there is a bug when splicing ~e, it emits an error with value 'apply' is not a member of scala.quoted.Expr[Unit] (see #4044).

@nicolasstucki
Copy link
Contributor Author

Fix will be possible after changes in #4081

@nicolasstucki
Copy link
Contributor Author

Fixed by #4081

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

No branches or pull requests

2 participants