How can I resolve a type provided by given in a Scala 3 macro?

48 Views Asked by At

I want to reflect on a class that uses Neotype (https://github.com/kitlangton/neotype/tree/main). This handy library allows you to define types that have built-in validation, and is used like this:

type NonEmptyString = NonEmptyString.Type
given NonEmptyString: Newtype[String] with
  inline def validate(input: String): Boolean =
    input.nonEmpty

// use
case class Person(name: NonEmptyString)

Ok, so if I reflect on Person, and get to the name field, I have a TypeRef for that field. Poking around in that TypeRep I find a lot of generic stuff--but nothing getting me to Newtype[String]. Here's the raw TypeRef:

TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class run)),object Sample$package),type NonEmptyString)
classSymbol: Some(class Any)
termSymbol: val <none>
typeArgs: List()
typeSymbol: type NonEmptyString

None of that is very helpful beyond telling me there's a type named NonEmptyString defined here. I'm concerned what I'm reflecting on is actually just the type definition:

type NonEmptyString = NonEmptyString.Type

and that it's not associated to the actual body of code in the given clause at the point I'm looking at it (compile-time in the macro).

The gold I seek is to be able to associate NonEmptyString type with its wrapped type, String. That's so I know the "real" underlying type and how to treat it.

How can I get from NonEmptyString.Type to Newtype[String] within my macro body using Quotes?

0

There are 0 best solutions below