Skip to content

Commit 075b7d1

Browse files
EugeneFlesselleodersky
authored andcommitted
Refine bounds of type Filter predicate
to only require being defined on the element types. Similar to what we have for `type FlatMap`
1 parent 0ab9e7b commit 075b7d1

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

library/src/scala/Tuple.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ sealed trait Tuple extends Product:
9494
* for which the given type level predicate `P` reduces to the literal
9595
* constant `true`.
9696
*/
97-
inline def filter[This >: this.type <: Tuple, P[_] <: Boolean]: Filter[This, P] =
97+
inline def filter[This >: this.type <: Tuple, P[_ <: Union[This]] <: Boolean]: Filter[This, P] =
9898
val toInclude = constValueTuple[IndicesWhere[This, P]].toArray
9999
val arr = new Array[Object](toInclude.length)
100100
for i <- 0 until toInclude.length do
@@ -216,7 +216,7 @@ object Tuple:
216216
* ```
217217
* @syntax markdown
218218
*/
219-
type Filter[X <: Tuple, P[_] <: Boolean] <: Tuple = X match
219+
type Filter[X <: Tuple, P[_ <: Union[X]] <: Boolean] <: Tuple = X match
220220
case EmptyTuple => EmptyTuple
221221
case x *: xs => P[x] match
222222
case true => x *: Filter[xs, P]
@@ -225,7 +225,7 @@ object Tuple:
225225
/** A tuple consisting of those indices `N` of tuple `X` where the predicate `P`
226226
* is true for `Elem[X, N]`. Indices are type level values <: Int.
227227
*/
228-
type IndicesWhere[X <: Tuple, P[_] <: Boolean] =
228+
type IndicesWhere[X <: Tuple, P[_ <: Union[X]] <: Boolean] =
229229
helpers.IndicesWhereHelper[X, P, 0]
230230

231231
/** The type of the tuple consisting of all element values of
@@ -355,7 +355,7 @@ object Tuple:
355355
private object helpers:
356356

357357
/** Used to implement IndicesWhere */
358-
type IndicesWhereHelper[X <: Tuple, P[_] <: Boolean, N <: Int] <: Tuple = X match
358+
type IndicesWhereHelper[X <: Tuple, P[_ <: Union[X]] <: Boolean, N <: Int] <: Tuple = X match
359359
case EmptyTuple => EmptyTuple
360360
case h *: t => P[h] match
361361
case true => N *: IndicesWhereHelper[t, P, S[N]]

tests/pos/tuple-filter.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ def Test =
88
summon[Tuple.Filter[(1, 2, 3, 4), P] =:= (1, 2, 4)]
99
summon[Tuple.Filter[(1, 2, 3, 4), RejectAll] =:= EmptyTuple]
1010
summon[Tuple.Filter[EmptyTuple, P] =:= EmptyTuple]
11+
12+
import compiletime.ops.int.<
13+
summon[Tuple.Filter[(1, 4, 7, 2, 10, 3, 4), [X <: Int] =>> X < 5] =:= (1, 4, 2, 3, 4)]

0 commit comments

Comments
 (0)