-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from alexarchambault/transparent
Use whitebox / transparent macros to have dependency literals return more refined types
- Loading branch information
Showing
16 changed files
with
393 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
dependency/src/main/scala-2.12/dependency/CovariantSet.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package dependency | ||
|
||
import scala.collection.immutable.SortedSet | ||
import scala.collection.mutable.Builder | ||
import scala.collection.IterableLike | ||
import scala.collection.generic.CanBuildFrom | ||
import scala.collection.AbstractIterable | ||
import scala.collection.generic.GenericTraversableTemplate | ||
import scala.collection.generic.GenericCompanion | ||
|
||
class CovariantSet[+A] private (private val elements: List[A]) | ||
extends AbstractIterable[A] | ||
with GenericTraversableTemplate[A, CovariantSet] | ||
with IterableLike[A, CovariantSet[A]] { | ||
|
||
override def companion: GenericCompanion[CovariantSet] = CovariantSet | ||
|
||
def +=[B >: A](elem: B): CovariantSet[B] = | ||
if (elements.contains(elem)) | ||
this | ||
else | ||
new CovariantSet[B](elem :: elements) | ||
def iterator: Iterator[A] = | ||
elements.iterator | ||
|
||
// override def newBuilder[A]: Builder[A, CovariantSet[A]] = ??? | ||
|
||
override def equals(obj: Any): Boolean = | ||
if (obj == null) false | ||
else if (this eq obj.asInstanceOf[AnyRef]) true | ||
else if (obj.isInstanceOf[CovariantSet[_]]) { | ||
val other = obj.asInstanceOf[CovariantSet[_]] | ||
elements.toSet == other.elements.toSet | ||
} | ||
else super.equals(obj) | ||
|
||
} | ||
|
||
object CovariantSet extends GenericCompanion[CovariantSet] { | ||
|
||
implicit def cbf[A, B]: CanBuildFrom[CovariantSet[A], B, CovariantSet[B]] = | ||
new CanBuildFrom[CovariantSet[A], B, CovariantSet[B]] { | ||
def apply(): scala.collection.mutable.Builder[B,dependency.CovariantSet[B]] = | ||
newBuilder[B] | ||
def apply(from: dependency.CovariantSet[A]): scala.collection.mutable.Builder[B,dependency.CovariantSet[B]] = | ||
newBuilder[B] | ||
} | ||
|
||
def newBuilder[A]: Builder[A, CovariantSet[A]] = | ||
new Builder[A, CovariantSet[A]] { | ||
val underlying = collection.mutable.Set.newBuilder[A] | ||
def +=(elem: A): this.type = { | ||
underlying += elem | ||
this | ||
} | ||
def clear(): Unit = underlying.clear() | ||
def result(): CovariantSet[A] = new CovariantSet[A](underlying.result().toList) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
dependency/src/main/scala-2.13/dependency/CovariantSet.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dependency | ||
|
||
import scala.collection.IterableOps | ||
import scala.collection.IterableFactoryDefaults | ||
import scala.collection.IterableFactory | ||
import scala.collection.mutable.Builder | ||
|
||
class CovariantSet[+A] private (private val elements: List[A]) | ||
extends Iterable[A] | ||
with IterableOps[A, CovariantSet, CovariantSet[A]] | ||
with IterableFactoryDefaults[A, CovariantSet] { | ||
|
||
def +=[B >: A](elem: B): CovariantSet[B] = | ||
if (elements.contains(elem)) | ||
this | ||
else | ||
new CovariantSet[B](elem :: elements) | ||
def iterator: Iterator[A] = | ||
elements.iterator | ||
|
||
override def iterableFactory: IterableFactory[CovariantSet] = | ||
CovariantSet | ||
|
||
override def equals(obj: Any): Boolean = | ||
if (obj == null) false | ||
else if (this eq obj.asInstanceOf[AnyRef]) true | ||
else if (obj.isInstanceOf[CovariantSet[_]]) { | ||
val other = obj.asInstanceOf[CovariantSet[_]] | ||
elements.toSet == other.elements.toSet | ||
} | ||
else super.equals(obj) | ||
|
||
} | ||
|
||
object CovariantSet extends IterableFactory[CovariantSet] { | ||
def from[A](source: IterableOnce[A]): CovariantSet[A] = | ||
new CovariantSet[A](source.toList.distinct) | ||
def empty[A]: CovariantSet[A] = | ||
new CovariantSet[A](Nil) | ||
def newBuilder[A]: Builder[A, CovariantSet[A]] = | ||
new Builder[A, CovariantSet[A]] { | ||
val underlying = collection.mutable.Set.newBuilder[A] | ||
def addOne(elem: A): this.type = { | ||
underlying.addOne(elem) | ||
this | ||
} | ||
def clear(): Unit = underlying.clear() | ||
def result(): CovariantSet[A] = new CovariantSet[A](underlying.result().toList) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dependency | ||
|
||
import scala.collection.IterableOps | ||
import scala.collection.IterableFactoryDefaults | ||
import scala.collection.IterableFactory | ||
import scala.collection.mutable.Builder | ||
|
||
class CovariantSet[+A] private (private val elements: List[A]) | ||
extends Iterable[A] | ||
with IterableOps[A, CovariantSet, CovariantSet[A]] | ||
with IterableFactoryDefaults[A, CovariantSet] { | ||
|
||
def +=[B >: A](elem: B): CovariantSet[B] = | ||
if (elements.contains(elem)) | ||
this | ||
else | ||
new CovariantSet[B](elem :: elements) | ||
def iterator: Iterator[A] = | ||
elements.iterator | ||
|
||
override def iterableFactory: IterableFactory[CovariantSet] = | ||
CovariantSet | ||
|
||
override def equals(obj: Any): Boolean = | ||
if (obj == null) false | ||
else if (this eq obj.asInstanceOf[AnyRef]) true | ||
else if (obj.isInstanceOf[CovariantSet[_]]) { | ||
val other = obj.asInstanceOf[CovariantSet[_]] | ||
elements.toSet == other.elements.toSet | ||
} | ||
else super.equals(obj) | ||
|
||
} | ||
|
||
object CovariantSet extends IterableFactory[CovariantSet] { | ||
def from[A](source: IterableOnce[A]): CovariantSet[A] = | ||
new CovariantSet[A](source.toList.distinct) | ||
def empty[A]: CovariantSet[A] = | ||
new CovariantSet[A](Nil) | ||
def newBuilder[A]: Builder[A, CovariantSet[A]] = | ||
new Builder[A, CovariantSet[A]] { | ||
val underlying = collection.mutable.Set.newBuilder[A] | ||
def addOne(elem: A): this.type = { | ||
underlying.addOne(elem) | ||
this | ||
} | ||
def clear(): Unit = underlying.clear() | ||
def result(): CovariantSet[A] = new CovariantSet[A](underlying.result().toList) | ||
} | ||
} |
Oops, something went wrong.