From 794e15fc23ca4a5dc9ac8937aa72b660d7fb1aa9 Mon Sep 17 00:00:00 2001 From: Jon Pretty Date: Sun, 5 Nov 2017 12:02:02 +0100 Subject: Further improvements to codegen size --- core/src/main/scala/magnolia.scala | 49 +++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'core/src/main/scala') diff --git a/core/src/main/scala/magnolia.scala b/core/src/main/scala/magnolia.scala index 1029d0c..9c8891e 100644 --- a/core/src/main/scala/magnolia.scala +++ b/core/src/main/scala/magnolia.scala @@ -6,12 +6,22 @@ import language.existentials import language.higherKinds import language.experimental.macros -abstract class Subclass[Tc[_], T](label: String) { +trait Subclass[Tc[_], T] { type S <: T + def label: String def typeclass: Tc[S] def cast: PartialFunction[T, S] } +object Subclass { + def apply[Tc[_], T, S1 <: T](name: String, tc: => Tc[S1], pf: => PartialFunction[T, S1]) = new Subclass[Tc, T] { + type S = S1 + def label: String = name + def typeclass: Tc[S] = tc + def cast: PartialFunction[T, S] = pf + } +} + object Param { def apply[Tc[_], T, S1](name: String, tc: Tc[S1], deref: T => S1) = new Param[Tc, T] { type S = S1 @@ -180,32 +190,27 @@ object Magnolia { c.abort(c.enclosingPosition, "") } - - Some { - - val subclasses = subtypes.map { searchType => - recurse(CoproductType(genericType.toString), genericType, assignedName) { - (searchType, typeclassTree(None, searchType, typeConstructor, assignedName)) - }.getOrElse { - c.abort(c.enclosingPosition, s"failed to get implicit for type $searchType") - } - }.map { case (typ, typeclass) => - val pf = q"""new _root_.scala.PartialFunction[$genericType, $typ] { - def isDefinedAt(t: $genericType): Boolean = t.isInstanceOf[$typ] - def apply(t: $genericType): $typ = t.asInstanceOf[$typ] - }""" - - q"""new _root_.magnolia.Subclass[$typeConstructor, $genericType](${typ.typeSymbol.name.toString}) { - type S = $typ - def typeclass: ${appliedType(typeConstructor, typ)} = $typeclass - def cast: _root_.scala.PartialFunction[$genericType, $typ] = $pf - }""" + + val subclasses = subtypes.map { searchType => + recurse(CoproductType(genericType.toString), genericType, assignedName) { + (searchType, typeclassTree(None, searchType, typeConstructor, assignedName)) + }.getOrElse { + c.abort(c.enclosingPosition, s"failed to get implicit for type $searchType") } + }.map { case (typ, typeclass) => + val pf = q""" + new _root_.scala.PartialFunction[$genericType, $typ] { + def isDefinedAt(t: $genericType): Boolean = t.isInstanceOf[$typ] + def apply(t: $genericType): $typ = t.asInstanceOf[$typ] + }""" + + q"""_root_.magnolia.Subclass[$typeConstructor, $genericType, $typ](${typ.typeSymbol.name.toString}, $typeclass, $pf)""" + } + Some { q"""{ ${c.prefix}.split(_root_.scala.collection.immutable.List[_root_.magnolia.Subclass[$typeConstructor, $genericType]](..$subclasses)) }""" - } } else None -- cgit v1.2.3