
com.avsystem.commons.macros.misc.SealedMacros.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-macros_2.12.0-RC2 Show documentation
Show all versions of commons-macros_2.12.0-RC2 Show documentation
AVSystem commons library for Scala
The newest version!
package com.avsystem.commons
package macros.misc
import com.avsystem.commons.macros.AbstractMacroCommons
import scala.reflect.macros.blackbox
/**
* Author: ghik
* Created: 11/12/15.
*/
class SealedMacros(ctx: blackbox.Context) extends AbstractMacroCommons(ctx) {
import c.universe._
val OrderedEnumType = getType(tq"$CommonsPackage.misc.OrderedEnum")
def caseObjectsFor[T: c.WeakTypeTag]: Tree = {
val tpe = weakTypeOf[T]
knownSubtypes(tpe).map { subtypes =>
val objects = subtypes.map(subTpe => singleValueFor(subTpe)
.getOrElse(abort(s"All possible values of a SealedEnum must be objects but $subTpe is not")))
val result = withKnownSubclassesCheck(q"$ListObj(..$objects)", tpe)
if (tpe <:< OrderedEnumType) q"$result.sorted" else result
}.getOrElse(abort(s"$tpe is not a sealed trait or class"))
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy