2

It is so easy in Scala 2, just:

q"lazy val $name = $value"

I need it for local values, so I don't have multi-thread requirements

I've tried just adding Flags.Lazy to the val definition equivalent:

val symbol = Symbol.newVal(
  Symbol.spliceOwner,
  name,
  TypeRepr.of[T],
  Flags.Lazy,
  Symbol.noSymbol
)
ValDef(symbol, Some(value.asTerm.changeOwner(symbol)))

But it doesn't work in Scala 3.3.6 (the latest LTS) and values generated by it fail to compile with the following error: "x is a forward reference extending over the definition of x"

While Scala 2 version works as expected helping me to resolve the forward reference issue

2
  • 2
    Are you using lazy val to handle recursion in definition? Is it possible that after building the whole tree definitions might be used out of order? I checked that I am building lazy vals in exactly the same way, they are working and -Xcheck-macros does not complain. Commented Jun 19 at 9:59
  • Please provide reproduction of x is a forward reference extending over the definition of x Commented Jun 19 at 22:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.