Hello, I’m encountering an issue when comparing the types of values defined in an object, when those values has an opaque type.
My original code does something else, but let’s make it simple.
I have some opaque defined in another file
opaque type OpaqueString = String
def OpaqueString(s: String): OpaqueString = s
and macro
inline def anyValHasType[T](value: Any): Unit = ${ anyValHasTypeImpl[T]('{ value }) }
def anyValHasType Impl[T: Type](value: Expr[Any])(using quotes: Quotes): Expr[Unit] = {
import quotes.reflect.*
val tpe = TypeRepr.of[T]
if !value.asTerm.tpe.termSymbol.fieldMembers.exists(_.typeRef <:< tpe) then {
report.error("No value of type " + tpe.show + " found in " + value.show, value.asTerm.pos)
}
'{ () }
}
And I use it like this:
object obj {
val i: Int = ???
val o: OpaqueString = ???
}
anyValHasType[Int](obj)
anyValHasType[OpaqueString](obj) // error
I tried with dealiasing, widening etc., but nothing helps.
Should I access the type of val
in another way? It works fine, when I change the macro definition and pass obj.o
directly.
Any suggestions on how to properly access or compare the type of a val
defined as an opaque type within an object?