Skip to content

Commit 1693271

Browse files
committed
Hylolib: Switch to member based type classes
1 parent ab7a521 commit 1693271

File tree

8 files changed

+24
-27
lines changed

8 files changed

+24
-27
lines changed

tests/pos/hylolib/AnyCollection.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object AnyCollection {
1919
// and `anyValueIsValue` when the method is called on a collection of `Int`s. None of these
2020
// choices is even correct! Note also that the ambiguity is suppressed if the constructor of
2121
// `AnyValue` is declared with a context bound rather than an implicit parameter.
22-
given Value[b.Position] = b.positionIsValue
22+
given b.Position is Value = b.positionIsValue
2323

2424
def start(): AnyValue =
2525
AnyValue(base.startPosition)
@@ -42,7 +42,7 @@ object AnyCollection {
4242

4343
}
4444

45-
given anyCollectionIsCollection[T: Value]: Collection[AnyCollection[T]] with {
45+
given [T: Value] => AnyCollection[T] is Collection with {
4646

4747
type Element = T
4848
type Position = AnyValue

tests/pos/hylolib/AnyValue.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ object AnyValue {
5858

5959
}
6060

61-
given anyValueIsValue: Value[AnyValue] with {
61+
given AnyValue is Value:
6262

63-
extension (self: AnyValue) {
63+
extension (self: AnyValue)
6464

6565
def copy(): AnyValue =
6666
self.copy()
@@ -71,6 +71,3 @@ given anyValueIsValue: Value[AnyValue] with {
7171
def hashInto(hasher: Hasher): Hasher =
7272
self.hashInto(hasher)
7373

74-
}
75-
76-
}

tests/pos/hylolib/BitArray.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ object BitArray {
318318

319319
}
320320

321-
given bitArrayPositionIsValue: Value[BitArray.Position] with {
321+
given BitArray.Position is Value with {
322322

323323
extension (self: BitArray.Position) {
324324

@@ -335,7 +335,7 @@ given bitArrayPositionIsValue: Value[BitArray.Position] with {
335335

336336
}
337337

338-
given bitArrayIsCollection: Collection[BitArray] with {
338+
given BitArray is Collection with {
339339

340340
type Element = Boolean
341341
type Position = BitArray.Position

tests/pos/hylolib/Collection.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
package hylo
33

44
/** A collection of elements accessible by their position. */
5-
trait Collection[Self] {
5+
trait Collection {
6+
type Self
67

78
/** The type of the elements in the collection. */
89
type Element: Value
@@ -213,7 +214,7 @@ extension [Self: Collection as s](self: Self) {
213214
* @complexity
214215
* O(n) where n is the number of elements in `self`.
215216
*/
216-
def minElement()(using Comparable[s.Element]): Option[s.Element] =
217+
def minElement()(using s.Element is Comparable): Option[s.Element] =
217218
self.minElement(isLessThan = _ `lt` _)
218219

219220
/** Returns the maximum element in `self`, using `isGreaterThan` to compare elements.
@@ -229,7 +230,7 @@ extension [Self: Collection as s](self: Self) {
229230
* @complexity
230231
* O(n) where n is the number of elements in `self`.
231232
*/
232-
def maxElement()(using Comparable[s.Element]): Option[s.Element] =
233+
def maxElement()(using s.Element is Comparable): Option[s.Element] =
233234
self.maxElement(isGreaterThan = _ `gt` _)
234235

235236
/** Returns the maximum element in `self`, using `isOrderedBefore` to compare elements.
@@ -257,12 +258,12 @@ extension [Self: Collection as s](self: Self) {
257258

258259
}
259260

260-
extension [Self: Collection as s](self: Self)(using
261-
Value[s.Element]
261+
extension [Self: Collection as s](self: Self)(
262+
using s.Element is Value
262263
) {
263264

264265
/** Returns `true` if `self` contains the same elements as `other`, in the same order. */
265-
def elementsEqual[T](using o: Collection[T] { type Element = s.Element })(other: T): Boolean =
266+
def elementsEqual[T](using o: T is Collection { type Element = s.Element })(other: T): Boolean =
266267
def loop(i: s.Position, j: o.Position): Boolean =
267268
if (i `eq` self.endPosition) {
268269
j `eq` other.endPosition

tests/pos/hylolib/CoreTraits.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ package hylo
55
* The data structure of and algorithms of Hylo's standard library operate "notional values" rather
66
* than arbitrary references. This trait defines the basis operations of all values.
77
*/
8-
trait Value[Self] {
8+
trait Value:
9+
type Self
910

1011
extension (self: Self) {
1112

@@ -15,20 +16,18 @@ trait Value[Self] {
1516
/** Returns `true` iff `self` and `other` have an equivalent value. */
1617
def eq(other: Self): Boolean
1718

19+
def neq(other: Self): Boolean = !self.eq(other)
20+
1821
/** Hashes the salient parts of `self` into `hasher`. */
1922
def hashInto(hasher: Hasher): Hasher
2023

2124
}
2225

23-
}
24-
25-
extension [Self: Value](self: Self) def neq(other: Self): Boolean = !self.eq(other)
26-
2726
// ----------------------------------------------------------------------------
2827
// Comparable
2928
// ----------------------------------------------------------------------------
3029

31-
trait Comparable[Self] extends Value[Self] {
30+
trait Comparable extends Value {
3231

3332
extension (self: Self) {
3433

tests/pos/hylolib/HyArray.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ object HyArray {
161161

162162
}
163163

164-
given [T: Value] => Value[HyArray[T]] with {
164+
given [T: Value] => HyArray[T] is Value with {
165165

166166
extension (self: HyArray[T]) {
167167

@@ -178,7 +178,7 @@ given [T: Value] => Value[HyArray[T]] with {
178178

179179
}
180180

181-
given [T: Value] => Collection[HyArray[T]] with {
181+
given [T: Value] => HyArray[T] is Collection with {
182182

183183
type Element = T
184184
type Position = Int

tests/pos/hylolib/Integers.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package hylo
22

3-
given booleanIsValue: Value[Boolean] with {
3+
given Boolean is Value with {
44

55
extension (self: Boolean) {
66

@@ -18,7 +18,7 @@ given booleanIsValue: Value[Boolean] with {
1818

1919
}
2020

21-
given intIsValue: Value[Int] with {
21+
given Int is Value with {
2222

2323
extension (self: Int) {
2424

@@ -36,7 +36,7 @@ given intIsValue: Value[Int] with {
3636

3737
}
3838

39-
given intIsComparable: Comparable[Int] with {
39+
given Int is Comparable with {
4040

4141
extension (self: Int) {
4242

tests/pos/hylolib/Slice.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class Slice[Base: Collection as b](
2424

2525
}
2626

27-
given sliceIsCollection[T: Collection as c]: Collection[Slice[T]] with {
27+
given [T: Collection as c] => Slice[T] is Collection with {
2828

2929
type Element = c.Element
3030
type Position = c.Position

0 commit comments

Comments
 (0)