Skip to content

How to get a type of a case class from a reference type of its companion object #14154

Discussion options

You must be logged in to vote

to do this, you can get the prefix of the type of the companion object, then use select on the prefix with the case class symbol, you need to explicitly refine the type of the anonymous class also to extract a usable CC type from the returned value (I use aux pattern also, but unnecessary):

import scala.quoted.*

trait CaseClass[Companion <: AnyRef]:
  type CC <: Product

object CaseClass:

  type Aux[Companion <: AnyRef, Case <: Product] = CaseClass[Companion] { type CC = Case }

  transparent inline given [Comp <: AnyRef]: CaseClass[Comp] = ${
    macroImpl[Comp]
  }
  def macroImpl[Comp <: AnyRef](using
      Quotes,
      Type[Comp]
  ): Expr[CaseClass[Comp]] =
    import quotes.reflect.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by soronpo
Comment options

soronpo
Dec 21, 2021
Collaborator Author

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants