Skip to content

Commit ae6fb3c

Browse files
committed
Hylolib: Drop usages of with in givens
1 parent 7175a21 commit ae6fb3c

File tree

6 files changed

+47
-98
lines changed

6 files changed

+47
-98
lines changed

tests/pos/hylolib/AnyCollection.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ object AnyCollection {
4242

4343
}
4444

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

4747
type Element = T
4848
type Position = AnyValue
4949

50-
extension (self: AnyCollection[T]) {
50+
extension (self: AnyCollection[T])
5151

5252
def startPosition =
5353
self._start()
@@ -61,6 +61,3 @@ given [T: Value] => AnyCollection[T] is Collection with {
6161
def at(p: Position) =
6262
self._at(p)
6363

64-
}
65-
66-
}

tests/pos/hylolib/BitArray.scala

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

319319
}
320320

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

323-
extension (self: BitArray.Position) {
323+
extension (self: BitArray.Position)
324324

325325
def copy(): BitArray.Position =
326326
self.copy()
@@ -331,16 +331,12 @@ given BitArray.Position is Value with {
331331
def hashInto(hasher: Hasher): Hasher =
332332
self.hashInto(hasher)
333333

334-
}
335-
336-
}
337-
338-
given BitArray is Collection with {
334+
given BitArray is Collection:
339335

340336
type Element = Boolean
341337
type Position = BitArray.Position
342338

343-
extension (self: BitArray) {
339+
extension (self: BitArray)
344340

345341
override def count: Int =
346342
self.count
@@ -357,16 +353,10 @@ given BitArray is Collection with {
357353
def at(p: BitArray.Position): Boolean =
358354
self.at(p)
359355

360-
}
361-
362-
}
363-
364-
given bitArrayIsStringConvertible: StringConvertible[BitArray] with {
365-
356+
given BitArray is StringConvertible:
366357
extension (self: BitArray)
367358
override def description: String =
368359
var contents = mutable.StringBuilder()
369360
self.forEach((e) => { contents += (if e then '1' else '0'); true })
370361
contents.mkString
371362

372-
}

tests/pos/hylolib/HyArray.scala

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ final class HyArray[Element: Value as elementIsCValue](
5757
result
5858

5959
// NOTE: Can't refine `C.Element` without renaming the generic parameter of `HyArray`.
60-
// /** Adds the contents of `source` at the end of the array. */
61-
// def appendContents[C](using
62-
// s: Collection[C]
63-
// )(
64-
// source: C { type Element = Element },
65-
// assumeUniqueness: Boolean = false
66-
// ): HyArray[Element] =
67-
// val result = if (assumeUniqueness) { this } else { copy(count + source.count) }
68-
// source.reduce(result, (r, e) => r.append(e, assumeUniqueness = true))
60+
/** Adds the contents of `source` at the end of the array. */
61+
def appendContents[C: Collection { type Element = HyArray.this.Element }](
62+
source: C, assumeUniqueness: Boolean = false
63+
): HyArray[Element] =
64+
val result = if (assumeUniqueness) { this } else { copy(count + source.count) }
65+
source.reduce(result): (r, e) =>
66+
r.append(e, assumeUniqueness = true)
6967

7068
/** Removes and returns the last element, or returns `None` if the array is empty. */
7169
def popLast(assumeUniqueness: Boolean = false): (HyArray[Element], Option[Element]) =
@@ -161,9 +159,9 @@ object HyArray {
161159

162160
}
163161

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

166-
extension (self: HyArray[T]) {
164+
extension (self: HyArray[T])
167165

168166
def copy(): HyArray[T] =
169167
self.copy()
@@ -174,16 +172,12 @@ given [T: Value] => HyArray[T] is Value with {
174172
def hashInto(hasher: Hasher): Hasher =
175173
self.reduce(hasher)((h, e) => e.hashInto(h))
176174

177-
}
178-
179-
}
180-
181-
given [T: Value] => HyArray[T] is Collection with {
175+
given [T: Value] => HyArray[T] is Collection:
182176

183177
type Element = T
184178
type Position = Int
185179

186-
extension (self: HyArray[T]) {
180+
extension (self: HyArray[T])
187181

188182
// NOTE: Having to explicitly override means that primary declaration can't automatically
189183
// specialize trait requirements.
@@ -199,22 +193,11 @@ given [T: Value] => HyArray[T] is Collection with {
199193

200194
def at(p: Int) = self.at(p)
201195

202-
}
203-
204-
}
205-
206-
// NOTE: This should work.
207-
// given hyArrayIsStringConvertible[T](using
208-
// tIsValue: Value[T],
209-
// tIsStringConvertible: StringConvertible[T]
210-
// ): StringConvertible[HyArray[T]] with {
211-
//
212-
// given Collection[HyArray[T]] = hyArrayIsCollection[T]
213-
//
214-
// extension (self: HyArray[T])
215-
// override def description: String =
216-
// var contents = mutable.StringBuilder()
217-
// self.forEach((e) => { contents ++= e.description; true })
218-
// s"[${contents.mkString(", ")}]"
219-
//
220-
// }
196+
given [T: {Value, StringConvertible}] => HyArray[T] is StringConvertible:
197+
extension (self: HyArray[T])
198+
override def description: String =
199+
val contents = mutable.StringBuilder()
200+
self.forEach: e =>
201+
contents ++= e.description
202+
true
203+
s"[${contents.mkString(", ")}]"

tests/pos/hylolib/Integers.scala

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

3-
given Boolean is Value with {
3+
given Boolean is Value:
44

5-
extension (self: Boolean) {
5+
extension (self: Boolean)
66

77
def copy(): Boolean =
88
// Note: Scala's `Boolean` has value semantics already.
@@ -14,13 +14,9 @@ given Boolean is Value with {
1414
def hashInto(hasher: Hasher): Hasher =
1515
hasher.combine(if self then 1 else 0)
1616

17-
}
17+
given Int is Value:
1818

19-
}
20-
21-
given Int is Value with {
22-
23-
extension (self: Int) {
19+
extension (self: Int)
2420

2521
def copy(): Int =
2622
// Note: Scala's `Int` has value semantics already.
@@ -32,13 +28,9 @@ given Int is Value with {
3228
def hashInto(hasher: Hasher): Hasher =
3329
hasher.combine(self)
3430

35-
}
36-
37-
}
31+
given Int is Comparable:
3832

39-
given Int is Comparable with {
40-
41-
extension (self: Int) {
33+
extension (self: Int)
4234

4335
def copy(): Int =
4436
self
@@ -51,8 +43,4 @@ given Int is Comparable with {
5143

5244
def lt(other: Int): Boolean = self < other
5345

54-
}
55-
56-
}
57-
58-
given intIsStringConvertible: StringConvertible[Int] with {}
46+
given Int is StringConvertible {}

tests/pos/hylolib/Slice.scala

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

33
/** A view into a collection. */
4-
final class Slice[Base: Collection as b](
5-
val base: Base,
6-
val bounds: Range[b.Position]
4+
final class Slice[Base: Collection](
5+
tracked val base: Base,
6+
tracked val bounds: Range[Base.Position]
77
) {
88

99
/** Returns `true` iff `this` is empty. */
1010
def isEmpty: Boolean =
1111
bounds.lowerBound.eq(bounds.upperBound)
1212

13-
def startPosition: b.Position =
13+
def startPosition: Base.Position =
1414
bounds.lowerBound
1515

16-
def endPosition: b.Position =
16+
def endPosition: Base.Position =
1717
bounds.upperBound
1818

19-
def positionAfter(p: b.Position): b.Position =
19+
def positionAfter(p: Base.Position): Base.Position =
2020
base.positionAfter(p)
2121

22-
def at(p: b.Position): b.Element =
22+
def at(p: Base.Position): Base.Element =
2323
base.at(p)
2424

2525
}
2626

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

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

32-
extension (self: Slice[T]) {
32+
extension (self: Slice[T])
3333

3434
def startPosition = self.bounds.lowerBound.asInstanceOf[Position] // NOTE: Ugly hack
3535

@@ -38,7 +38,3 @@ given [T: Collection as c] => Slice[T] is Collection with {
3838
def positionAfter(p: Position) = self.base.positionAfter(p)
3939

4040
def at(p: Position) = self.base.at(p)
41-
42-
}
43-
44-
}
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package hylo
22

33
/** A type whose instances can be described by a character string. */
4-
trait StringConvertible[Self] {
4+
trait StringConvertible:
5+
type Self
56

6-
extension (self: Self) {
7-
8-
/** Returns a textual description of `self`. */
9-
def description: String =
10-
self.toString
11-
12-
}
13-
14-
}
7+
/** Returns a textual description of `self`. */
8+
extension (self: Self)
9+
def description: String = self.toString

0 commit comments

Comments
 (0)