Skip to content

Commit 308569c

Browse files
Do indexOf runtime operation based on term equality
and refine `type IndexOf` doc
1 parent a825ef4 commit 308569c

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

library/src/scala/Tuple.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ object Tuple:
176176
infix type ++[X <: Tuple, +Y <: Tuple] = Concat[X, Y]
177177

178178
/** The index of `Y` in tuple `X` as a literal constant Int,
179-
* or `Size[X]` if `Y` does not occur in `X`
179+
* or `Size[X]` if `Y` is disjoint from all element types in `X`.
180180
*/
181181
type IndexOf[X <: Tuple, Y] <: Int = X match
182182
case Y *: _ => 0
@@ -332,21 +332,22 @@ object Tuple:
332332
runtime.Tuples.fromProduct(product)
333333

334334
extension [X <: Tuple](inline x: X)
335+
// Note the two methods are not equivalent to using `constValue`,
336+
// since they also allow cases unknown at compiletime.
337+
// Also note it would be unsound to use a type parameter for `y` in the type level
338+
// operations, since they are rightfully not covariant in their second parameter.
335339

336340
/** The index (starting at 0) of the first occurrence of y.type in the type `X` of `x`
337341
* or Size[X] if no such element exists.
338342
*/
339-
transparent inline def indexOf(y: Any): Int = constValue[IndexOf[X, y.type]]
343+
inline def indexOf(y: Any): IndexOf[X, y.type] =
344+
x.productIterator.indexOf(y).asInstanceOf[IndexOf[X, y.type]]
340345

341346
/** A boolean indicating whether there is an element `y.type` in the type `X` of `x` */
342-
// Note this isn't equivalent to `constValue[Contains[X, y.type]]`
343-
// since it also accepts cases unknown at compiletime.
344-
// Also note it would be unsound to use a type parameter for `y` in the
345-
// type level `Contains`, since it is rightfully not covariant in `Y`.
346347
inline def contains(y: Any): Contains[X, y.type] =
347348
x.productIterator.contains(y).asInstanceOf[Contains[X, y.type]]
348349

349-
// TODO containsType ?
350+
// TODO indexOfType & containsType ?
350351

351352
end extension
352353

0 commit comments

Comments
 (0)