diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-01-12 20:28:43 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2018-01-12 20:28:43 -0800 |
commit | 9cca64627d31f078c565c6865a50ae558f567d8f (patch) | |
tree | 6ac4630f51aec69acf3140d858fa1b3298f0e5fc /core/src/main/scala | |
parent | aa0cee34da02cfe7e477a9c9b716a592c6809406 (diff) | |
download | mill-9cca64627d31f078c565c6865a50ae558f567d8f.tar.gz mill-9cca64627d31f078c565c6865a50ae558f567d8f.tar.bz2 mill-9cca64627d31f078c565c6865a50ae558f567d8f.zip |
Allow implicit `crossModule()` syntax to automatically find a set of cross-coordinates which are compatible with a given implicit resolver
Diffstat (limited to 'core/src/main/scala')
-rw-r--r-- | core/src/main/scala/mill/define/Cross.scala | 29 | ||||
-rw-r--r-- | core/src/main/scala/mill/discover/Discovered.scala | 2 |
2 files changed, 27 insertions, 4 deletions
diff --git a/core/src/main/scala/mill/define/Cross.scala b/core/src/main/scala/mill/define/Cross.scala index 6226590d..2dd4d2fe 100644 --- a/core/src/main/scala/mill/define/Cross.scala +++ b/core/src/main/scala/mill/define/Cross.scala @@ -26,10 +26,15 @@ object Cross{ reify { mill.define.Cross.Factory[T](instance.splice) } } } + + trait Resolver[-T]{ + def resolve[V <: T](c: Cross[V]): V + } } + class Cross[T](cases: Any*) - (implicit ci: Cross.Factory[T], - val ctx: Module.Ctx){ + (implicit ci: Cross.Factory[T], + val ctx: Module.Ctx){ val items = for(c0 <- cases.toList) yield{ val c = c0 match{ @@ -47,5 +52,23 @@ class Cross[T](cases: Any*) (crossValues, sub) } val itemMap = items.toMap - def apply(args: Any*) = itemMap(args.toList) + + /** + * Fetch the cross module corresponding to the given cross values + */ + def get(args: Seq[Any]) = itemMap(args.toList) + + /** + * Fetch the cross module corresponding to the given cross values + */ + def apply(arg0: Any, args: Any*) = itemMap(arg0 :: args.toList) + + /** + * Fetch the relevant cross module given the implicit resolver you have in + * scope. This is often the first cross module whose cross-version is + * compatible with the current module. + */ + def apply[V >: T]()(implicit resolver: Cross.Resolver[V]): T = { + resolver.resolve(this.asInstanceOf[Cross[V]]).asInstanceOf[T] + } }
\ 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 0728a027..c5ca4843 100644 --- a/core/src/main/scala/mill/discover/Discovered.scala +++ b/core/src/main/scala/mill/discover/Discovered.scala @@ -112,7 +112,7 @@ object Discovered { val base = q"${TermName(c.freshName())}" val ident = segments.reverse.zipWithIndex.foldLeft[Tree](base) { case (prefix, (Some(name), i)) => q"$prefix.${TermName(name)}" - case (prefix, (None, i)) => q"$prefix.apply($crossName($i):_*)" + case (prefix, (None, i)) => q"$prefix.get($crossName($i))" } q"($base: $baseType, $crossName: List[List[Any]]) => $ident.asInstanceOf[$t]" } |