summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-09-14 15:23:51 +0200
committerEugene Burmako <xeno.by@gmail.com>2012-09-15 00:55:27 +0200
commit210f8c8f108aaf650a5be640c4db0d2336193f33 (patch)
tree6939dd3043ff7dd2af840e9d8a66b2e612c6d78d /src/library
parented913c2963c472440b06a3a4cfa3d2853cea21d4 (diff)
downloadscala-210f8c8f108aaf650a5be640c4db0d2336193f33.tar.gz
scala-210f8c8f108aaf650a5be640c4db0d2336193f33.tar.bz2
scala-210f8c8f108aaf650a5be640c4db0d2336193f33.zip
SI-6372 cleans up the API of Attachments
Previously Attachments allowed multiple attachments that correspond to the same attachment type. This created a potential for confusion, given that Attachments.get only searched for the first attachment of a given type. Hence I made Attachments.add overwrite previously existing attachments of a given type and renamed it to Attachments.update, so that the name suits the intention better.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/reflect/base/Attachments.scala11
1 files changed, 7 insertions, 4 deletions
diff --git a/src/library/scala/reflect/base/Attachments.scala b/src/library/scala/reflect/base/Attachments.scala
index 43e870fc4f..889ac0ac14 100644
--- a/src/library/scala/reflect/base/Attachments.scala
+++ b/src/library/scala/reflect/base/Attachments.scala
@@ -20,15 +20,18 @@ abstract class Attachments { self =>
/** Gets the underlying payload */
def all: Set[Any] = Set.empty
+ private def matchesTag[T: ClassTag](datum: Any) =
+ classTag[T].runtimeClass == datum.getClass
+
def get[T: ClassTag]: Option[T] =
- (all find (_.getClass == classTag[T].runtimeClass)).asInstanceOf[Option[T]]
+ (all filter matchesTag[T]).headOption.asInstanceOf[Option[T]]
/** Creates a copy of this attachment with its payload updated */
- def add(attachment: Any): Attachments { type Pos = self.Pos } =
- new NonemptyAttachments(this.pos, all + attachment)
+ def update[T: ClassTag](attachment: T): Attachments { type Pos = self.Pos } =
+ new NonemptyAttachments(this.pos, remove[T].all + attachment)
def remove[T: ClassTag]: Attachments { type Pos = self.Pos } = {
- val newAll = all filterNot (_.getClass == classTag[T].runtimeClass)
+ val newAll = all filterNot matchesTag[T]
if (newAll.isEmpty) pos.asInstanceOf[Attachments { type Pos = self.Pos }]
else new NonemptyAttachments(this.pos, newAll)
}