diff options
author | Jon Pretty <jon.pretty@propensive.com> | 2017-06-22 18:28:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-22 18:28:30 +0100 |
commit | 27a55611230b772eae1f0a37822decb43c21ac25 (patch) | |
tree | 3613b2a36440d026fa9b2efa184d255b0e59b98d /core | |
parent | 70919a58e73e928d1e2c04a22a0636897e45816c (diff) | |
parent | 6e702a5a5fb9f9d508e0a9ddbbbfc2cb64267971 (diff) | |
download | magnolia-27a55611230b772eae1f0a37822decb43c21ac25.tar.gz magnolia-27a55611230b772eae1f0a37822decb43c21ac25.tar.bz2 magnolia-27a55611230b772eae1f0a37822decb43c21ac25.zip |
Merge pull request #5 from propensive/use-case-instead-of-asinstanceof
Use case clauses and switch statement instead of is/asInstanceOf
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/scala/magnolia.scala | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/core/src/main/scala/magnolia.scala b/core/src/main/scala/magnolia.scala index 27172d3..66d59d0 100644 --- a/core/src/main/scala/magnolia.scala +++ b/core/src/main/scala/magnolia.scala @@ -161,18 +161,6 @@ class Macros(val c: whitebox.Context) { val reduction = components.reduce { (left, right) => q"$impl.combine($left, $right)" } q"$impl.call($reduction, sourceParameter)" - case ContravariantDerivation1Implicit(impl) => - val parts = subtypes.tail.zip(components.tail) - val base = q""" - $impl.call(${components.head}, sourceParameter.asInstanceOf[${subtypes.head}]) - """ - parts.foldLeft(base) { case (aggregated, (componentType, derivedImplicit)) => - q""" - if(sourceParameter.isInstanceOf[$componentType]) - $impl.call($derivedImplicit, sourceParameter.asInstanceOf[$componentType]) - else $aggregated""" - } - case ContravariantDerivation2Implicit(impl) => val parts = subtypes.tail.zip(components.tail) val base = q""" @@ -184,6 +172,16 @@ class Macros(val c: whitebox.Context) { $impl.call($derivedImplicit, sourceParameter1.asInstanceOf[$componentType], sourceParameter2.asInstanceOf[$componentType]) else $aggregated""" } + case ContravariantDerivation1Implicit(impl) => + val parts = subtypes.zip(components) + + val caseClauses = parts.map { case (subtype, component) => + cq"(value: $subtype) => $impl.call($component, value)" + } + + q"""(sourceParameter: @_root_.scala.annotation.switch) match { + case ..$caseClauses + }""" } } } else None |