question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Quoted var assignment

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
nicolasstuckicommented, Feb 28, 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).

0reactions
nicolasstuckicommented, Mar 16, 2018

Fixed by #4081

Read more comments on GitHub >

github_iconTop Results From Across the Web

Are quotes needed for local variable assignment?
Quotes are needed in export foo="$var" or local foo="$var" (or readonly , typeset , declare and other variable declaring commands) in:.
Read more >
bash - Quoting vs not quoting the variable on the RHS of a ...
I think there is no big difference here. Yes, it is advisable to enclose a variable in double quotes when that variable is...
Read more >
4.2. Variable Assignment
Variable Assignment. = the assignment operator (no space before and after) ... tabs and newlines. echo echo "$a" # The quoted variable preserves...
Read more >
Forgetting Quotes Leads to “command not found” on ... - O'Reilly
Your script is assigning some values to a variable, but when you run it, the shell reports “command not found” on part of...
Read more >
Understanding Shell Script Variables
This assigns the string "Hello World" to the variable MY_MESSAGE then echo es out the value of the variable. Note that we need...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found