aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Pretty <jon.pretty@propensive.com>2018-01-08 20:30:05 +0000
committerJon Pretty <jon.pretty@propensive.com>2018-01-08 20:30:05 +0000
commit2353f9f1d3961a29a1d7b6173f5fd22d7acf0f47 (patch)
tree164138a38bfb0be6a1543eac43e56d7d3f62b572
parenta295b25480c5c9207198315f2c2161897bf8cba5 (diff)
downloadmagnolia-2353f9f1d3961a29a1d7b6173f5fd22d7acf0f47.tar.gz
magnolia-2353f9f1d3961a29a1d7b6173f5fd22d7acf0f47.tar.bz2
magnolia-2353f9f1d3961a29a1d7b6173f5fd22d7acf0f47.zip
Generalised getTypeMember method
-rw-r--r--core/shared/src/main/scala/magnolia.scala20
1 files changed, 12 insertions, 8 deletions
diff --git a/core/shared/src/main/scala/magnolia.scala b/core/shared/src/main/scala/magnolia.scala
index e0ebdbb..759deb6 100644
--- a/core/shared/src/main/scala/magnolia.scala
+++ b/core/shared/src/main/scala/magnolia.scala
@@ -84,15 +84,19 @@ object Magnolia {
val prefixObject = prefixType.typeSymbol
val prefixName = prefixObject.name.decodedName
- val typeDefs = prefixType.baseClasses.flatMap { cls =>
- cls.asType.toType.decls.filter(_.isType).find(_.name.toString == "Typeclass").map { tpe =>
- tpe.asType.toType.asSeenFrom(prefixType, cls)
+ def getTypeMember(typeMember: String) = {
+ val typeDefs = prefixType.baseClasses.flatMap { cls =>
+ cls.asType.toType.decls.filter(_.isType).find(_.name.toString == typeMember).map { tpe =>
+ tpe.asType.toType.asSeenFrom(prefixType, cls)
+ }
+ }
+
+ typeDefs.headOption.map(_.typeConstructor).getOrElse {
+ fail(s"the derivation $prefixObject does not define the Typeclass $typeMember type constructor")
}
}
- val typeConstructor = typeDefs.headOption.fold {
- fail(s"the derivation $prefixObject does not define the Typeclass type constructor")
- }(_.typeConstructor)
+ val typeclassConstructor = getTypeMember("Typeclass")
def checkMethod(termName: String, category: String, expected: String): Unit = {
val term = TermName(termName)
@@ -352,7 +356,7 @@ object Magnolia {
}
val genericType: Type = weakTypeOf[T]
- val searchType = appliedType(typeConstructor, genericType)
+ val searchType = appliedType(typeclassConstructor, genericType)
val directlyReentrant = stack.top.exists(_.searchType =:= searchType)
if (directlyReentrant) throw DirectlyReentrantException()
@@ -362,7 +366,7 @@ object Magnolia {
q"$magnoliaPkg.Deferred[$searchType](${enclosingRef.toString})"
}
.orElse {
- directInferImplicit(genericType, typeConstructor)
+ directInferImplicit(genericType, typeclassConstructor)
}
for (tree <- result) if (debug.isDefined && genericType.toString.contains(debug.get)) {