Skip to content

Add an Ordering given instance for named tuples #23379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions library/src/scala/NamedTuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ object NamedTuple:
/** The empty named tuple */
val Empty: Empty = EmptyTuple

/** The ordering instance for named tuples */
given namedTupleOrdering: [N <: Tuple, V <: Tuple] => (ord: Ordering[V]) => Ordering[NamedTuple[N, V]]:
def compare(x: NamedTuple[N, V], y: NamedTuple[N, V]): Int =
ord.compare(x.toTuple, y.toTuple)
Comment on lines +132 to +134
Copy link
Contributor

@JD557 JD557 Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but is it worth constructing this manually, when there's already a Ordering.by helper for situations like this?

given namedTupleOrdering: [N <: Tuple, V <: Tuple] => Ordering[V] => Ordering[NamedTuple[N, V]] =
  Ordering.by(_.toTuple)

Not that it makes much of a difference, but should be slightly less error prone (e.g. for sure the x and y won't be switched).

end NamedTuple

/** Separate from NamedTuple object so that we can match on the opaque type NamedTuple. */
Expand Down
3 changes: 3 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ object MiMaFilters {

ProblemFilters.exclude[DirectMissingMethodProblem]("scala.Conversion.underlying"),
ProblemFilters.exclude[MissingClassProblem]("scala.Conversion$"),

ProblemFilters.exclude[DirectMissingMethodProblem]("scala.NamedTuple.namedTupleOrdering"),
ProblemFilters.exclude[MissingClassProblem]("scala.NamedTuple$namedTupleOrdering"),
),

// Additions since last LTS
Expand Down
1 change: 1 addition & 0 deletions tests/run/named-tuple-ordering.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
List((Alice,29), (Alice,30), (Bob,25), (Charlie,35))
13 changes: 13 additions & 0 deletions tests/run/named-tuple-ordering.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Person = (name: String, age: Int)

val so = summon[Ordering[Person]]

val people: List[Person] = List(
(name = "Charlie", age = 35),
(name = "Alice", age = 30),
(name = "Alice", age = 29),
(name = "Bob", age = 25),
)
val sortedPeople = people.sorted

@main def Test = println(s"$sortedPeople")
Loading