Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Easier access to type wrapped type from within a Scala 3 macro #38

Open
gzoller opened this issue Apr 18, 2024 · 0 comments
Open

Comments

@gzoller
Copy link

gzoller commented Apr 18, 2024

If you're trying to instantiate a NeoType from inside a macro, say from parsed input, it's very hard (at least I haven't found a way) to get type 'B', the wrapped type. It's not lying about someplace easy to get to accessible to Quotes. (You need the wrapped type to know how to parse/marshal raw input content into a typed thing.)

I figured that if I can navigate my way to validate() the input param to validate is the unwrapped type, so...

  val _module = Symbol.requiredModule(typedName.toString) // the NeoType
  val _validate = _module.methodMember("validate").head // validate method
  println("NeoTypeRef: " + _module + "\n")
  println("Validate Unhelpful: " + _validate.paramSymss + "\n") // Should be what I need here... but sadly, no...
  println("Validate Helpful: " + _validate.info + "\n")
  _validate.info match
    case MethodType(_, paramss, _) => // paramss.head is exactly what I want: the TypeRepr of the wrapped type
      println(paramss.head)

This produces:

NeoTypeRef: object NonEmptyList

Validate Unhelpful: List(List(val input))

Validate Helpful: MethodType(List(input), List(AppliedType(TypeRef(ThisType(TypeRef(NoPrefix,module class immutable)),class List),List(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int)))), TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Boolean))

AppliedType(TypeRef(ThisType(TypeRef(NoPrefix,module class immutable)),class List),List(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int)))

You can see the "normal" validate paramSymss is completely unhelpful--not sure why. The second info--the one with with the good stuff in it--required the -experimental flag to be set. All well and good, but who knows how stable that behavior will be in the future.

Is there away to more explicitly expose the wrapped type within a macro? Or is it already there and I haven't found the recipe to get to it yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant