Hello,
I’m trying to get an optional inline value using this code:
package io.github.iltotore.scalalint
import scala.quoted._
object compileTime {
given fromExpr[T]: FromExpr[T] with {
def unapply(expr: Expr[T])(using Quotes) =
import quotes.reflect._
def rec(tree: Term): Option[T] = tree match {
case Block(stats, e) => if stats.isEmpty then rec(e) else None
case Inlined(_, bindings, e) => if bindings.isEmpty then rec(e) else None
case Typed(e, _) => rec(e)
case _ =>
tree.tpe.widenTermRefByName match {
case ConstantType(c) => Some(c.value.asInstanceOf[T])
case _ => None
}
}
rec(expr.asTerm)
}
inline def inlineValueOpt[T](expr: T): Option[T] = ${inlineValueOptImpl('{expr})}
private def inlineValueOptImpl[T](expr: Expr[T])(using Quotes, Type[T]): Expr[Option[T]] = {
def recursiveSearch[T](expr: Expr[T])(using Quotes, Type[T], FromExpr[T]): Expr[Option[T]] = {
expr match {
case '{val name: T = $next} => recursiveSearch(next)
case _ =>
val value = expr.value
'{value}
}
}
recursiveSearch(expr)
}
}
But I don’t understand this error:
[error] -- Error: C:\Users\rapha\IdeaProjects\ScalaLint\main\src\io\github\iltotore\scalalint\compileTime.scala:34:12
[error] 34 | '{value}
[error] | ^^^^^
[error] | access to value value from wrong staging level:
[error] | - the definition is at level 0,
[error] | - but the access is at level 1.
What is this and how can I fix it ? I did some research and the only answer I found was for a very specific case.