Skip to content

Commit 19f2280

Browse files
authored
Merge pull request scala/scala#6972 from cb372/productElementName
Product element name
2 parents e359147 + 222fac4 commit 19f2280

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/src/scala/Product.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,24 @@ trait Product extends Any with Equals {
4848
* @return in the default implementation, the empty string
4949
*/
5050
def productPrefix = ""
51+
52+
/** The name of the n^th^ element of this product, 0-based.
53+
* In the default implementation, an empty string.
54+
*
55+
* @param n the index of the element name to return
56+
* @throws IndexOutOfBoundsException
57+
* @return the name of the specified element
58+
*/
59+
def productElementName(n: Int): String =
60+
if (n >= 0 && n < productArity) ""
61+
else throw new IndexOutOfBoundsException(n.toString)
62+
63+
/** An iterator over the names of all the elements of this product.
64+
*/
65+
def productElementNames: Iterator[String] = new scala.collection.AbstractIterator[String] {
66+
private[this] var c: Int = 0
67+
private[this] val cmax = productArity
68+
def hasNext = c < cmax
69+
def next() = { val result = productElementName(c); c += 1; result }
70+
}
5171
}

0 commit comments

Comments
 (0)