diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-01-08 00:19:46 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2018-01-08 00:36:31 -0800 |
commit | d037fcb99dfc94691783094d811cb60f2241aebb (patch) | |
tree | 4b845eaadda65ca42dda0b7cb9f0c02842226d01 /core/src/main/scala | |
parent | 5aa363015646644cc81afdf0120d8df441161e2d (diff) | |
download | mill-d037fcb99dfc94691783094d811cb60f2241aebb.tar.gz mill-d037fcb99dfc94691783094d811cb60f2241aebb.tar.bz2 mill-d037fcb99dfc94691783094d811cb60f2241aebb.zip |
Consolidate `Cross` and `CrossModule`, eliminate now-unused `map`/`flatMap`/`filter` APIs
Diffstat (limited to 'core/src/main/scala')
-rw-r--r-- | core/src/main/scala/mill/define/Cross.scala | 45 | ||||
-rw-r--r-- | core/src/main/scala/mill/discover/Discovered.scala | 6 | ||||
-rw-r--r-- | core/src/main/scala/mill/main/ReplApplyHandler.scala | 5 | ||||
-rw-r--r-- | core/src/main/scala/mill/package.scala | 2 |
4 files changed, 16 insertions, 42 deletions
diff --git a/core/src/main/scala/mill/define/Cross.scala b/core/src/main/scala/mill/define/Cross.scala index 4b0e1b8f..72424bde 100644 --- a/core/src/main/scala/mill/define/Cross.scala +++ b/core/src/main/scala/mill/define/Cross.scala @@ -1,35 +1,9 @@ package mill.define import language.experimental.macros -import scala.reflect.ClassTag -import scala.reflect.macros.{Context, blackbox} +import scala.reflect.macros.blackbox -case class Cross[+T](items: List[(List[Any], T)])(implicit val e: sourcecode.Enclosing, val l: sourcecode.Line){ - def flatMap[V](f: T => Cross[V]): Cross[V] = new Cross( - items.flatMap{ - case (l, v) => f(v).items.map{case (l2, v2) => (l2 ::: l, v2)} - } - ) - def map[V](f: T => V): Cross[V] = new Cross(items.map{case (l, v) => (l, f(v))}) - def withFilter(f: T => Boolean): Cross[T] = new Cross(items.filter(t => f(t._2))) - def applyOpt(input: Any*): Option[T] = { - val inputList = input.toList - items.find(_._1 == inputList).map(_._2) - } - def apply(input: Any*): T = { - applyOpt(input:_*).getOrElse( - throw new Exception( - "Unknown set of cross values: " + input + - " not in known values\n" + items.map(_._1).mkString("\n") - ) - ) - } -} object Cross{ - def apply[T](t: T*) = new Cross(t.map(i => List(i) -> i).toList) -} - -object CrossModule{ def autoCast[A](x: Any): A = x.asInstanceOf[A] abstract class Implicit[T]{ def make(v: Any, ctx: Module.Ctx): T @@ -47,7 +21,7 @@ object CrossModule{ val tree = primaryConstructorArgs match{ case List(arg) => q""" - new mill.define.CrossModule.Implicit[$tpe]{ + new mill.define.Cross.Implicit[$tpe]{ def make(v: Any, ctx0: mill.define.Module.Ctx) = new $tpe(v.asInstanceOf[${arg.info}]){ override def ctx = ctx0 } @@ -59,7 +33,7 @@ object CrossModule{ q"v.asInstanceOf[scala.Product].productElement($n).asInstanceOf[${a.info}]" } q""" - new mill.define.CrossModule.Implicit[$tpe]{ + new mill.define.Cross.Implicit[$tpe]{ def make(v: Any, ctx0: mill.define.Module.Ctx) = new $tpe(..$argTupleValues){ override def ctx = ctx0 } @@ -72,11 +46,10 @@ object CrossModule{ } } } -class CrossModule[T](cases: Any*) - (implicit ci: CrossModule.Implicit[T], - ctx: Module.Ctx) -extends Cross[T]({ - for(c <- cases.toList) yield{ +class Cross[T](cases: Any*) + (implicit ci: Cross.Implicit[T], + val ctx: Module.Ctx){ + val items = for(c <- cases.toList) yield{ val crossValues = ci.crossValues(c) val sub = ci.make( c, @@ -87,4 +60,6 @@ extends Cross[T]({ ) (crossValues.reverse, sub) } -})
\ No newline at end of file + val itemMap = items.toMap + def apply(args: Any*) = itemMap(args.toList) +}
\ No newline at end of file diff --git a/core/src/main/scala/mill/discover/Discovered.scala b/core/src/main/scala/mill/discover/Discovered.scala index 74312f91..49d2db4a 100644 --- a/core/src/main/scala/mill/discover/Discovered.scala +++ b/core/src/main/scala/mill/discover/Discovered.scala @@ -103,10 +103,10 @@ object Discovered { } val crossChildren = - if (!(t <:< c.weakTypeOf[Cross[Module]])) q"None" + if (!(t <:< c.weakTypeOf[Cross[_]])) q"None" else { - val TypeRef(_, _, Seq(arg)) = t.baseType(typeOf[Cross[Module]].typeSymbol) + val TypeRef(_, _, Seq(arg)) = t.baseType(weakTypeOf[Cross[_]].typeSymbol) val innerMirror = rec(None :: segments, arg) q"Some(((c: mill.define.Cross[_]) => mill.discover.Discovered.tupleLeft(c.items), $innerMirror))" } @@ -114,7 +114,7 @@ object Discovered { m <- t.members.toList if m.typeSignature.paramLists.isEmpty && m.isPublic if (m.typeSignature.finalResultType <:< c.weakTypeOf[Module]) || - (m.typeSignature.finalResultType <:< c.weakTypeOf[Cross[Module]]) + (m.typeSignature.finalResultType <:< c.weakTypeOf[Cross[_]]) } yield { val name = m.name.toString.trim diff --git a/core/src/main/scala/mill/main/ReplApplyHandler.scala b/core/src/main/scala/mill/main/ReplApplyHandler.scala index 77926d45..0bcf8e6e 100644 --- a/core/src/main/scala/mill/main/ReplApplyHandler.scala +++ b/core/src/main/scala/mill/main/ReplApplyHandler.scala @@ -2,8 +2,7 @@ package mill.main import mill.define.Applicative.ApplyHandler -import mill.define.{Cross, Target, Task} -import mill.define.Segment +import mill.define._ import mill.discover.{Discovered, Mirror} import mill.eval.Evaluator import mill.util.OSet @@ -43,7 +42,7 @@ class ReplApplyHandler(pprinter0: pprint.PPrinter, evaluator: Evaluator[_]) exte val millHandlers: PartialFunction[Any, pprint.Tree] = { case c: Cross[_] => pprint.Tree.Lazy( ctx => - Iterator(c.e.value, ":", c.l.value.toString, ctx.applyPrefixColor("\nChildren:").toString) ++ + Iterator(c.ctx.enclosing , ":", c.ctx.lineNum.toString, ctx.applyPrefixColor("\nChildren:").toString) ++ c.items.iterator.map(x => "\n (" + x._1.map(pprint.PPrinter.BlackWhite.apply(_)).mkString(", ") + ")" ) diff --git a/core/src/main/scala/mill/package.scala b/core/src/main/scala/mill/package.scala index b578ed3b..3463b63f 100644 --- a/core/src/main/scala/mill/package.scala +++ b/core/src/main/scala/mill/package.scala @@ -7,5 +7,5 @@ package object mill extends JsonFormatters{ type PathRef = mill.eval.PathRef type Module = define.Module val Module = define.Module - type CrossModule[T] = define.CrossModule[T] + type Cross[T] = define.Cross[T] } |