Skip to content

Commit

Permalink
Refine the bounds of the Tuple.Filter type lambda predicate ..
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EugeneFlesselle committed Nov 11, 2024
1 parent cc38962 commit a1d7f5e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ object Tuple {
* ```
* @syntax markdown
*/
type Filter[Tup <: Tuple, P[_] <: Boolean] <: Tuple = Tup match {
type Filter[Tup <: Tuple, P[_ <: Union[Tup]] <: Boolean] <: Tuple = Tup match {
case EmptyTuple => EmptyTuple
case h *: t => P[h] match {
case true => h *: Filter[t, P]
Expand Down
12 changes: 12 additions & 0 deletions tests/neg/tuple-filter-compat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

type OldFilter[Tup <: Tuple, P[_] <: Boolean] = Nothing
type NewFilter[Tup <: Tuple, P[_ <: Tuple.Union[Tup]] <: Boolean] = Nothing

trait A:
type X >: OldFilter <: OldFilter

trait B1 extends A:
type X = OldFilter // ok

trait B2 extends A:
type X = NewFilter // error: breaking change
6 changes: 6 additions & 0 deletions tests/pos/tuple-filter.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import scala.compiletime.ops.int.<

type P[x] <: Boolean = x match {
case 3 => false
case _ => true
}

type RejectAll[x] = false

type Pos[X <: Int] = 0 < X

def Test =
summon[Tuple.Filter[(1, 2, 3, 4), P] =:= (1, 2, 4)]
summon[Tuple.Filter[(1, 2, 3, 4), RejectAll] =:= EmptyTuple]
summon[Tuple.Filter[EmptyTuple, P] =:= EmptyTuple]
summon[Tuple.Filter[(1, -2, 3, -4), Pos] =:= (1, 3)]

0 comments on commit a1d7f5e

Please sign in to comment.