From 0e6efd56d6f250925782b4d1af0f4b78f6699b5b Mon Sep 17 00:00:00 2001 From: Vince Reuter Date: Wed, 13 Nov 2024 13:15:18 +0100 Subject: [PATCH] add Order instance for AtLeast2 --- modules/pan/src/main/scala/collections.scala | 5 +++++ modules/pan/src/test/scala/TestAtLeast2.scala | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/modules/pan/src/main/scala/collections.scala b/modules/pan/src/main/scala/collections.scala index 7f33f72..97aa5b2 100644 --- a/modules/pan/src/main/scala/collections.scala +++ b/modules/pan/src/main/scala/collections.scala @@ -72,6 +72,11 @@ object collections: /** Define equality the same way as for the underlying, unrefined value. */ given eqForAtLeast2[C[*], E](using Eq[C[E]]): Eq[AtLeast2[C, E]] = Eq.by(es => es: C[E]) + /** Define order the same way as for the underlying, unrefined value. */ + given orderForAtLeast2[C[*], A](using Order[C[A]]): Order[AtLeast2[C, A]] = + Order.by: xs => + xs: C[A] + /** Simply concatenate the elements from the left list to the head of the right list. */ given semigroupKForAtLeast2List: SemigroupK[AtLeast2List] = new: override def combineK[A](xs: AtLeast2List[A], ys: AtLeast2List[A]): AtLeast2List[A] = diff --git a/modules/pan/src/test/scala/TestAtLeast2.scala b/modules/pan/src/test/scala/TestAtLeast2.scala index a48af91..8497b27 100644 --- a/modules/pan/src/test/scala/TestAtLeast2.scala +++ b/modules/pan/src/test/scala/TestAtLeast2.scala @@ -100,4 +100,13 @@ class TestAtLeast2 forAll: (xs: AtLeast2[Set, Int]) => xs.toNes shouldEqual NonEmptySet.fromSetUnsafe(xs.toSortedSet) + test("When syntax is imported, AtLeast2[Set, A] may be represented as simply Set[A]."): + assertCompiles("AtLeast2.unsafe(Set(1, 2))") // Precondition: we can build the collection. + assertTypeError( + "AtLeast2.unsafe(Set(1, 2)).toSet" + ) // Test: without syntax import, .toNes is unavailable. + assertCompiles( + "import AtLeast2.syntax.toSet; AtLeast2.unsafe(Set(1, 2)).toSet" + ) // Test: with syntax import, .toNes becomes available. + end TestAtLeast2