diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-04-27 17:11:36 +0200 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-05-02 12:30:28 +0200 |
commit | f004e99b2fb7450fbfd0c5d96a4b2406cb8fc142 (patch) | |
tree | 69e52be0bd8f9b823768daed95c0169acbea8df0 /src/library | |
parent | 90d2bee45b25844f809f8c5300aefcb1bfe9e336 (diff) | |
download | scala-f004e99b2fb7450fbfd0c5d96a4b2406cb8fc142.tar.gz scala-f004e99b2fb7450fbfd0c5d96a4b2406cb8fc142.tar.bz2 scala-f004e99b2fb7450fbfd0c5d96a4b2406cb8fc142.zip |
small tree attachment refactoring: firstAttachment
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/reflect/api/Trees.scala | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/src/library/scala/reflect/api/Trees.scala b/src/library/scala/reflect/api/Trees.scala index f1e9cc13ca..b82972c9bc 100644 --- a/src/library/scala/reflect/api/Trees.scala +++ b/src/library/scala/reflect/api/Trees.scala @@ -110,13 +110,12 @@ trait Trees { self: Universe => def withoutAttachment(att: Any): this.type = { detach(att); this } def attachment[T: ClassTag]: T = attachmentOpt[T] getOrElse { throw new Error("no attachment of type %s".format(classTag[T].erasure)) } def attachmentOpt[T: ClassTag]: Option[T] = + firstAttachment { case attachment if attachment.getClass == classTag[T].erasure => attachment.asInstanceOf[T] } + + def firstAttachment[T](p: PartialFunction[Any, T]): Option[T] = rawatt match { - case NontrivialAttachment(pos, payload) => - val index = payload.indexWhere(p => p.getClass == classTag[T].erasure) - if (index != -1) Some(payload(index).asInstanceOf[T]) - else None - case _ => - None + case NontrivialAttachment(pos, payload) => payload.collectFirst(p) + case _ => None } private[this] var rawtpe: Type = _ |