Skip to content

Commit 455058c

Browse files
Refine the bounds of the Tuple.Filter type lambda predicate ..
to only require it be defined on the elements of the tuple. This is one of the ongoing proposed tuple improvements, addressing #19175. As carefully pointed out by @sjrd, this _is_ a potential breaking change. See tests/neg/tuple-filter-compat.scala for an example. This is not an unprecedented change however, the analogous improvements were made to `Tuple.{Map, FlatMap}` in 28a695e.
1 parent 3dfd762 commit 455058c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Diff for: library/src/scala/Tuple.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ object Tuple {
166166
* ```
167167
* @syntax markdown
168168
*/
169-
type Filter[Tup <: Tuple, P[_] <: Boolean] <: Tuple = Tup match {
169+
type Filter[Tup <: Tuple, P[_ <: Union[Tup]] <: Boolean] <: Tuple = Tup match {
170170
case EmptyTuple => EmptyTuple
171171
case h *: t => P[h] match {
172172
case true => h *: Filter[t, P]

Diff for: tests/neg/tuple-filter-compat.scala

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
type OldFilter[Tup <: Tuple, P[_] <: Boolean] = Nothing
3+
type NewFilter[Tup <: Tuple, P[_ <: Tuple.Union[Tup]] <: Boolean] = Nothing
4+
5+
trait A:
6+
type X >: OldFilter <: OldFilter
7+
8+
trait B1 extends A:
9+
type X = OldFilter // ok
10+
11+
trait B2 extends A:
12+
type X = NewFilter // error: breaking change

Diff for: tests/pos/tuple-filter.scala

+6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import scala.compiletime.ops.int.<
2+
13
type P[x] <: Boolean = x match {
24
case 3 => false
35
case _ => true
46
}
7+
58
type RejectAll[x] = false
69

10+
type Pos[X <: Int] = 0 < X
11+
712
def Test =
813
summon[Tuple.Filter[(1, 2, 3, 4), P] =:= (1, 2, 4)]
914
summon[Tuple.Filter[(1, 2, 3, 4), RejectAll] =:= EmptyTuple]
1015
summon[Tuple.Filter[EmptyTuple, P] =:= EmptyTuple]
16+
summon[Tuple.Filter[(1, -2, 3, -4), Pos] =:= (1, 3)]

0 commit comments

Comments
 (0)