From 7b425bf7f02c15d6c3577f21318c6bdfc92d6b35 Mon Sep 17 00:00:00 2001 From: Vinicius Miana Date: Tue, 29 Jan 2013 14:14:02 -0200 Subject: SI-6370 changed ListMap apply0 method to produce correct error message when a key is not found Current implementation of apply0 relies on tail method to iterate over all keys. When the list gets to its end, tail produces an 'empty map' message in its exception, which is thrown by ListMap. This change checks if the collection is empty before calling tail and provides a more appropriate key not found message. Signed-off-by: Vinicius Miana --- src/library/scala/collection/immutable/ListMap.scala | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/library/scala/collection/immutable/ListMap.scala b/src/library/scala/collection/immutable/ListMap.scala index 670540187e..75817350e5 100644 --- a/src/library/scala/collection/immutable/ListMap.scala +++ b/src/library/scala/collection/immutable/ListMap.scala @@ -156,8 +156,12 @@ extends AbstractMap[A, B] * @return the value associated with the given key. */ override def apply(k: A): B1 = apply0(this, k) - - @tailrec private def apply0(cur: ListMap[A, B1], k: A): B1 = if (k == cur.key) cur.value else apply0(cur.tail, k) + + + @tailrec private def apply0(cur: ListMap[A, B1], k: A): B1 = + if (cur.isEmpty) throw new NoSuchElementException("key not found: "+k) + else if (k == cur.key) cur.value + else apply0(cur.tail, k) /** Checks if this map maps `key` to a value and return the * value if it exists. -- cgit v1.2.3