diff options
author | Jon Pretty <jon.pretty@propensive.com> | 2017-11-05 12:02:02 +0100 |
---|---|---|
committer | Jon Pretty <jon.pretty@propensive.com> | 2017-11-05 12:02:02 +0100 |
commit | 794e15fc23ca4a5dc9ac8937aa72b660d7fb1aa9 (patch) | |
tree | 8408142260b9512c71f817288c72606464c3dff1 /core/src/main/scala/magnolia.scala | |
parent | 2a65718d3dd0a4921b90d2d6c1e35cc921d50a43 (diff) | |
download | magnolia-794e15fc23ca4a5dc9ac8937aa72b660d7fb1aa9.tar.gz magnolia-794e15fc23ca4a5dc9ac8937aa72b660d7fb1aa9.tar.bz2 magnolia-794e15fc23ca4a5dc9ac8937aa72b660d7fb1aa9.zip |
Further improvements to codegen size
Diffstat (limited to 'core/src/main/scala/magnolia.scala')
-rw-r--r-- | core/src/main/scala/magnolia.scala | 49 |
1 files changed, 27 insertions, 22 deletions
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 |