Skip to content

Commit a162b7b

Browse files
Merge pull request #9336 from dotty-staging/use-new-extension-syntax-in-lib
Use new extension syntax in library
2 parents aa64078 + bee5749 commit a162b7b

File tree

4 files changed

+107
-105
lines changed

4 files changed

+107
-105
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class Definitions {
226226
@tu lazy val Compiletime_error : Symbol = CompiletimePackageObject.requiredMethod(nme.error)
227227
@tu lazy val Compiletime_constValue : Symbol = CompiletimePackageObject.requiredMethod("constValue")
228228
@tu lazy val Compiletime_constValueOpt: Symbol = CompiletimePackageObject.requiredMethod("constValueOpt")
229-
@tu lazy val Compiletime_code : Symbol = CompiletimePackageObject.requiredMethod("code")
229+
@tu lazy val Compiletime_code : Symbol = CompiletimePackageObject.requiredMethod("extension_code")
230230
@tu lazy val Compiletime_summonFrom : Symbol = CompiletimePackageObject.requiredMethod("summonFrom")
231231
@tu lazy val CompiletimeTestingPackageObject: Symbol = ctx.requiredModule("scala.compiletime.testing.package")
232232
@tu lazy val CompiletimeTesting_typeChecks: Symbol = CompiletimeTestingPackageObject.requiredMethod("typeChecks")

library/src/scala/IArray.scala

+68-68
Original file line numberDiff line numberDiff line change
@@ -19,242 +19,242 @@ object opaques:
1919
* @param n the index of the element to select
2020
* @return the element of the array at the given index
2121
*/
22-
def (arr: IArray[Byte]) apply(n: Int): Byte = arr.asInstanceOf[Array[Byte]].apply(n)
23-
def (arr: IArray[Short]) apply(n: Int): Short = arr.asInstanceOf[Array[Short]].apply(n)
24-
def (arr: IArray[Char]) apply(n: Int): Char = arr.asInstanceOf[Array[Char]].apply(n)
25-
def (arr: IArray[Int]) apply(n: Int): Int = arr.asInstanceOf[Array[Int]].apply(n)
26-
def (arr: IArray[Long]) apply(n: Int): Long = arr.asInstanceOf[Array[Long]].apply(n)
27-
def (arr: IArray[Float]) apply(n: Int): Float = arr.asInstanceOf[Array[Float]].apply(n)
28-
def (arr: IArray[Double]) apply(n: Int): Double = arr.asInstanceOf[Array[Double]].apply(n)
29-
def [T <: Object](arr: IArray[T]) apply (n: Int): T = arr.asInstanceOf[Array[T]].apply(n)
30-
def [T](arr: IArray[T]) apply (n: Int): T = arr.asInstanceOf[Array[T]].apply(n)
22+
extension (arr: IArray[Byte]) def apply(n: Int): Byte = arr.asInstanceOf[Array[Byte]].apply(n)
23+
extension (arr: IArray[Short]) def apply(n: Int): Short = arr.asInstanceOf[Array[Short]].apply(n)
24+
extension (arr: IArray[Char]) def apply(n: Int): Char = arr.asInstanceOf[Array[Char]].apply(n)
25+
extension (arr: IArray[Int]) def apply(n: Int): Int = arr.asInstanceOf[Array[Int]].apply(n)
26+
extension (arr: IArray[Long]) def apply(n: Int): Long = arr.asInstanceOf[Array[Long]].apply(n)
27+
extension (arr: IArray[Float]) def apply(n: Int): Float = arr.asInstanceOf[Array[Float]].apply(n)
28+
extension (arr: IArray[Double]) def apply(n: Int): Double = arr.asInstanceOf[Array[Double]].apply(n)
29+
extension [T <: Object](arr: IArray[T]) def apply (n: Int): T = arr.asInstanceOf[Array[T]].apply(n)
30+
extension [T](arr: IArray[T]) def apply (n: Int): T = arr.asInstanceOf[Array[T]].apply(n)
3131

3232
/** The number of elements in an immutable array
3333
* @param arr the immutable array
3434
*/
35-
def (arr: IArray[Byte]) length: Int = arr.asInstanceOf[Array[Byte]].length
36-
def (arr: IArray[Short]) length: Int = arr.asInstanceOf[Array[Short]].length
37-
def (arr: IArray[Char]) length: Int = arr.asInstanceOf[Array[Char]].length
38-
def (arr: IArray[Int]) length: Int = arr.asInstanceOf[Array[Int]].length
39-
def (arr: IArray[Long]) length: Int = arr.asInstanceOf[Array[Long]].length
40-
def (arr: IArray[Float]) length: Int = arr.asInstanceOf[Array[Float]].length
41-
def (arr: IArray[Double]) length: Int = arr.asInstanceOf[Array[Double]].length
42-
def (arr: IArray[Object]) length: Int = arr.asInstanceOf[Array[Object]].length
43-
def [T](arr: IArray[T]) length: Int = arr.asInstanceOf[Array[T]].length
35+
extension (arr: IArray[Byte]) def length: Int = arr.asInstanceOf[Array[Byte]].length
36+
extension (arr: IArray[Short]) def length: Int = arr.asInstanceOf[Array[Short]].length
37+
extension (arr: IArray[Char]) def length: Int = arr.asInstanceOf[Array[Char]].length
38+
extension (arr: IArray[Int]) def length: Int = arr.asInstanceOf[Array[Int]].length
39+
extension (arr: IArray[Long]) def length: Int = arr.asInstanceOf[Array[Long]].length
40+
extension (arr: IArray[Float]) def length: Int = arr.asInstanceOf[Array[Float]].length
41+
extension (arr: IArray[Double]) def length: Int = arr.asInstanceOf[Array[Double]].length
42+
extension (arr: IArray[Object]) def length: Int = arr.asInstanceOf[Array[Object]].length
43+
extension [T](arr: IArray[T]) def length: Int = arr.asInstanceOf[Array[T]].length
4444

4545
/** Returns this array concatenated with the given array. */
46-
def [T, U >: T: ClassTag](arr: IArray[T]) ++(that: IArray[U]): IArray[U] =
46+
extension [T, U >: T: ClassTag](arr: IArray[T]) def ++(that: IArray[U]): IArray[U] =
4747
genericArrayOps(arr) ++ that
4848

4949
/** Tests whether this array contains a given value as an element. */
50-
def [T](arr: IArray[T]) contains(elem: T): Boolean =
50+
extension [T](arr: IArray[T]) def contains(elem: T): Boolean =
5151
// `genericArrayOps(arr).contains(elem)` does not work because `elem` does not have type `arr.T`
5252
// but we can use `exists` instead, which is how `ArrayOps#contains` itself is implemented:
5353
genericArrayOps(arr).exists(_ == elem)
5454

5555
/** Counts the number of elements in this array which satisfy a predicate */
56-
def [T](arr: IArray[T]) count(p: T => Boolean): Int =
56+
extension [T](arr: IArray[T]) def count(p: T => Boolean): Int =
5757
genericArrayOps(arr).count(p)
5858

5959
/** The rest of the array without its `n` first elements. */
60-
def [T](arr: IArray[T]) drop(n: Int): IArray[T] =
60+
extension [T](arr: IArray[T]) def drop(n: Int): IArray[T] =
6161
genericArrayOps(arr).drop(n)
6262

6363
/** The rest of the array without its `n` last elements. */
64-
def [T](arr: IArray[T]) dropRight(n: Int): IArray[T] =
64+
extension [T](arr: IArray[T]) def dropRight(n: Int): IArray[T] =
6565
genericArrayOps(arr).dropRight(n)
6666

6767
/** Drops longest prefix of elements that satisfy a predicate. */
68-
def [T](arr: IArray[T]) dropWhile(p: T => Boolean): IArray[T] =
68+
extension [T](arr: IArray[T]) def dropWhile(p: T => Boolean): IArray[T] =
6969
genericArrayOps(arr).dropWhile(p)
7070

7171
/** Tests whether a predicate holds for at least one element of this array. */
72-
def [T](arr: IArray[T]) exists(p: T => Boolean): Boolean =
72+
extension [T](arr: IArray[T]) def exists(p: T => Boolean): Boolean =
7373
genericArrayOps(arr).exists(p)
7474

7575
/** Selects all elements of this array which satisfy a predicate. */
76-
def [T](arr: IArray[T]) filter(p: T => Boolean): IArray[T] =
76+
extension [T](arr: IArray[T]) def filter(p: T => Boolean): IArray[T] =
7777
genericArrayOps(arr).filter(p)
7878

7979
/** Selects all elements of this array which do not satisfy a predicate. */
80-
def [T](arr: IArray[T]) filterNot(p: T => Boolean): IArray[T] =
80+
extension [T](arr: IArray[T]) def filterNot(p: T => Boolean): IArray[T] =
8181
genericArrayOps(arr).filterNot(p)
8282

8383
/** Finds the first element of the array satisfying a predicate, if any. */
84-
def [T](arr: IArray[T]) find(p: T => Boolean): Option[T] =
84+
extension [T](arr: IArray[T]) def find(p: T => Boolean): Option[T] =
8585
genericArrayOps(arr).find(p)
8686

8787
/** Builds a new array by applying a function to all elements of this array
8888
* and using the elements of the resulting collections. */
89-
def [T, U: ClassTag](arr: IArray[T]) flatMap(f: T => IterableOnce[U]): IArray[U] =
89+
extension [T, U: ClassTag](arr: IArray[T]) def flatMap(f: T => IterableOnce[U]): IArray[U] =
9090
genericArrayOps(arr).flatMap(f)
9191

9292
/** Flattens a two-dimensional array by concatenating all its rows
9393
* into a single array. */
94-
def [T, U: ClassTag](arr: IArray[T]) flatten(using T => Iterable[U]): IArray[U] =
94+
extension [T, U: ClassTag](arr: IArray[T]) def flatten(using T => Iterable[U]): IArray[U] =
9595
genericArrayOps(arr).flatten
9696

9797
/** Folds the elements of this array using the specified associative binary operator. */
98-
def [T, U >: T: ClassTag](arr: IArray[T]) fold(z: U)(op: (U, U) => U): U =
98+
extension [T, U >: T: ClassTag](arr: IArray[T]) def fold(z: U)(op: (U, U) => U): U =
9999
genericArrayOps(arr).fold(z)(op)
100100

101101
/** Applies a binary operator to a start value and all elements of this array,
102102
* going left to right. */
103-
def [T, U: ClassTag](arr: IArray[T]) foldLeft(z: U)(op: (U, T) => U): U =
103+
extension [T, U: ClassTag](arr: IArray[T]) def foldLeft(z: U)(op: (U, T) => U): U =
104104
genericArrayOps(arr).foldLeft(z)(op)
105105

106106
/** Applies a binary operator to all elements of this array and a start value,
107107
* going right to left. */
108-
def [T, U: ClassTag](arr: IArray[T]) foldRight(z: U)(op: (T, U) => U): U =
108+
extension [T, U: ClassTag](arr: IArray[T]) def foldRight(z: U)(op: (T, U) => U): U =
109109
genericArrayOps(arr).foldRight(z)(op)
110110

111111
/** Tests whether a predicate holds for all elements of this array. */
112-
def [T](arr: IArray[T]) forall(p: T => Boolean): Boolean =
112+
extension [T](arr: IArray[T]) def forall(p: T => Boolean): Boolean =
113113
genericArrayOps(arr).forall(p)
114114

115115
/** Apply `f` to each element for its side effects. */
116-
def [T, U](arr: IArray[T]) foreach(f: T => U): Unit =
116+
extension [T, U](arr: IArray[T]) def foreach(f: T => U): Unit =
117117
genericArrayOps(arr).foreach(f)
118118

119119
/** Selects the first element of this array. */
120-
def [T](arr: IArray[T]) head: T =
120+
extension [T](arr: IArray[T]) def head: T =
121121
genericArrayOps(arr).head
122122

123123
/** Optionally selects the first element. */
124-
def [T](arr: IArray[T]) headOption: Option[T] =
124+
extension [T](arr: IArray[T]) def headOption: Option[T] =
125125
genericArrayOps(arr).headOption
126126

127127
/** Finds index of first occurrence of some value in this array after or at some start index. */
128-
def [T](arr: IArray[T]) indexOf(elem: T, from: Int = 0): Int =
128+
extension [T](arr: IArray[T]) def indexOf(elem: T, from: Int = 0): Int =
129129
// `asInstanceOf` needed because `elem` does not have type `arr.T`
130130
// We could use `arr.iterator.indexOf(elem, from)` or `arr.indexWhere(_ == elem, from)`
131131
// but these would incur some overhead.
132132
genericArrayOps(arr).indexOf(elem.asInstanceOf, from)
133133

134134
/** Finds index of the first element satisfying some predicate after or at some start index. */
135-
def [T](arr: IArray[T]) indexWhere(p: T => Boolean, from: Int = 0): Int =
135+
extension [T](arr: IArray[T]) def indexWhere(p: T => Boolean, from: Int = 0): Int =
136136
genericArrayOps(arr).indexWhere(p, from)
137137

138138
/** Produces the range of all indices of this sequence. */
139-
def [T](arr: IArray[T]) indices: Range =
139+
extension [T](arr: IArray[T]) def indices: Range =
140140
genericArrayOps(arr).indices
141141

142142
/** The initial part of the array without its last element. */
143-
def [T](arr: IArray[T]) init: IArray[T] =
143+
extension [T](arr: IArray[T]) def init: IArray[T] =
144144
genericArrayOps(arr).init
145145

146146
/** Tests whether the array is empty. */
147-
def [T](arr: IArray[T]) isEmpty: Boolean =
147+
extension [T](arr: IArray[T]) def isEmpty: Boolean =
148148
genericArrayOps(arr).isEmpty
149149

150150
/** An iterator yielding the elemenst of this array. */
151-
def [T](arr: IArray[T]) iterator: Iterator[T] =
151+
extension [T](arr: IArray[T]) def iterator: Iterator[T] =
152152
genericArrayOps(arr).iterator
153153

154154
/** Selects the last element. */
155-
def [T](arr: IArray[T]) last: T =
155+
extension [T](arr: IArray[T]) def last: T =
156156
genericArrayOps(arr).last
157157

158158
/** Optionally selects the last element. */
159-
def [T](arr: IArray[T]) lastOption: Option[T] =
159+
extension [T](arr: IArray[T]) def lastOption: Option[T] =
160160
genericArrayOps(arr).lastOption
161161

162162
/** Finds index of last occurrence of some value in this array before or at a given end index. */
163-
def [T](arr: IArray[T]) lastIndexOf(elem: T, end: Int = arr.length - 1): Int =
163+
extension [T](arr: IArray[T]) def lastIndexOf(elem: T, end: Int = arr.length - 1): Int =
164164
// see: same issue in `indexOf`
165165
genericArrayOps(arr).lastIndexOf(elem.asInstanceOf, end)
166166

167167
/** Finds index of last element satisfying some predicate before or at given end index. */
168-
def [T](arr: IArray[T]) lastIndexWhere(p: T => Boolean, end: Int = arr.length - 1): Int =
168+
extension [T](arr: IArray[T]) def lastIndexWhere(p: T => Boolean, end: Int = arr.length - 1): Int =
169169
genericArrayOps(arr).lastIndexWhere(p, end)
170170

171171
/** Builds a new array by applying a function to all elements of this array. */
172-
def [T, U: ClassTag](arr: IArray[T]) map(f: T => U): IArray[U] =
172+
extension [T, U: ClassTag](arr: IArray[T]) def map(f: T => U): IArray[U] =
173173
genericArrayOps(arr).map(f)
174174

175175
/** Tests whether the array is not empty. */
176-
def [T](arr: IArray[T]) nonEmpty: Boolean =
176+
extension [T](arr: IArray[T]) def nonEmpty: Boolean =
177177
genericArrayOps(arr).nonEmpty
178178

179179
/** A pair of, first, all elements that satisfy predicate `p` and, second, all elements that do not. */
180-
def [T](arr: IArray[T]) partition(p: T => Boolean): (IArray[T], IArray[T]) =
180+
extension [T](arr: IArray[T]) def partition(p: T => Boolean): (IArray[T], IArray[T]) =
181181
genericArrayOps(arr).partition(p)
182182

183183
/** Returns a new array with the elements in reversed order. */
184-
def [T](arr: IArray[T]) reverse: IArray[T] =
184+
extension [T](arr: IArray[T]) def reverse: IArray[T] =
185185
genericArrayOps(arr).reverse
186186

187187
/** Computes a prefix scan of the elements of the array. */
188-
def [T, U >: T: ClassTag](arr: IArray[T]) scan(z: U)(op: (U, U) => U): IArray[U] =
188+
extension [T, U >: T: ClassTag](arr: IArray[T]) def scan(z: U)(op: (U, U) => U): IArray[U] =
189189
genericArrayOps(arr).scan(z)(op)
190190

191191
/** Produces an array containing cumulative results of applying the binary
192192
* operator going left to right. */
193-
def [T, U: ClassTag](arr: IArray[T]) scanLeft(z: U)(op: (U, T) => U): IArray[U] =
193+
extension [T, U: ClassTag](arr: IArray[T]) def scanLeft(z: U)(op: (U, T) => U): IArray[U] =
194194
genericArrayOps(arr).scanLeft(z)(op)
195195

196196
/** Produces an array containing cumulative results of applying the binary
197197
* operator going right to left. */
198-
def [T, U: ClassTag](arr: IArray[T]) scanRight(z: U)(op: (T, U) => U): IArray[U] =
198+
extension [T, U: ClassTag](arr: IArray[T]) def scanRight(z: U)(op: (T, U) => U): IArray[U] =
199199
genericArrayOps(arr).scanRight(z)(op)
200200

201201
/** The size of this array. */
202-
def [T](arr: IArray[T]) size: Int =
202+
extension [T](arr: IArray[T]) def size: Int =
203203
arr.length
204204

205205
/** Selects the interval of elements between the given indices. */
206-
def [T](arr: IArray[T]) slice(from: Int, until: Int): IArray[T] =
206+
extension [T](arr: IArray[T]) def slice(from: Int, until: Int): IArray[T] =
207207
genericArrayOps(arr).slice(from, until)
208208

209209
/** Sorts this array according to the Ordering which results from transforming
210210
* an implicitly given Ordering with a transformation function. */
211-
def [T, U: ClassTag](arr: IArray[T]) sortBy(f: T => U)(using math.Ordering[U]): IArray[T] =
211+
extension [T, U: ClassTag](arr: IArray[T]) def sortBy(f: T => U)(using math.Ordering[U]): IArray[T] =
212212
genericArrayOps(arr).sortBy(f)
213213

214214
/** Sorts this array according to a comparison function. */
215-
def [T](arr: IArray[T]) sortWith(f: (T, T) => Boolean): IArray[T] =
215+
extension [T](arr: IArray[T]) def sortWith(f: (T, T) => Boolean): IArray[T] =
216216
genericArrayOps(arr).sortWith(f)
217217

218218
/** Sorts this array according to an Ordering. */
219-
def [T](arr: IArray[T]) sorted(using math.Ordering[T]): IArray[T] =
219+
extension [T](arr: IArray[T]) def sorted(using math.Ordering[T]): IArray[T] =
220220
genericArrayOps(arr).sorted
221221

222222
/** Splits this array into a prefix/suffix pair according to a predicate. */
223-
def [T](arr: IArray[T]) span(p: T => Boolean): (IArray[T], IArray[T]) =
223+
extension [T](arr: IArray[T]) def span(p: T => Boolean): (IArray[T], IArray[T]) =
224224
genericArrayOps(arr).span(p)
225225

226226
/** Splits this array into two at a given position. */
227-
def [T](arr: IArray[T]) splitAt(n: Int): (IArray[T], IArray[T]) =
227+
extension [T](arr: IArray[T]) def splitAt(n: Int): (IArray[T], IArray[T]) =
228228
genericArrayOps(arr).splitAt(n)
229229

230230
/** Tests whether this array starts with the given array. */
231-
def [T, U >: T: ClassTag](arr: IArray[T]) startsWith(that: IArray[U], offset: Int = 0): Boolean =
231+
extension [T, U >: T: ClassTag](arr: IArray[T]) def startsWith(that: IArray[U], offset: Int = 0): Boolean =
232232
genericArrayOps(arr).startsWith(that)
233233

234234
/** The rest of the array without its first element. */
235-
def [T](arr: IArray[T]) tail: IArray[T] =
235+
extension [T](arr: IArray[T]) def tail: IArray[T] =
236236
genericArrayOps(arr).tail
237237

238238
/** An array containing the first `n` elements of this array. */
239-
def [T](arr: IArray[T]) take(n: Int): IArray[T] =
239+
extension [T](arr: IArray[T]) def take(n: Int): IArray[T] =
240240
genericArrayOps(arr).take(n)
241241

242242
/** An array containing the last `n` elements of this array. */
243-
def [T](arr: IArray[T]) takeRight(n: Int): IArray[T] =
243+
extension [T](arr: IArray[T]) def takeRight(n: Int): IArray[T] =
244244
genericArrayOps(arr).takeRight(n)
245245

246246
/** Takes longest prefix of elements that satisfy a predicate. */
247-
def [T](arr: IArray[T]) takeWhile(p: T => Boolean): IArray[T] =
247+
extension [T](arr: IArray[T]) def takeWhile(p: T => Boolean): IArray[T] =
248248
genericArrayOps(arr).takeWhile(p)
249249

250250
/** Converts an array of pairs into an array of first elements and an array of second elements. */
251-
def [U: ClassTag, V: ClassTag](arr: IArray[(U, V)]) unzip: (IArray[U], IArray[V]) =
251+
extension [U: ClassTag, V: ClassTag](arr: IArray[(U, V)]) def unzip: (IArray[U], IArray[V]) =
252252
genericArrayOps(arr).unzip
253253

254254
/** Returns an array formed from this array and another iterable collection
255255
* by combining corresponding elements in pairs.
256256
* If one of the two collections is longer than the other, its remaining elements are ignored. */
257-
def [T, U: ClassTag](arr: IArray[T]) zip(that: IArray[U]): IArray[(T, U)] =
257+
extension [T, U: ClassTag](arr: IArray[T]) def zip(that: IArray[U]): IArray[(T, U)] =
258258
genericArrayOps(arr).zip(that)
259259
}
260260
end opaques

library/src/scala/compiletime/package.scala

+20-18
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,26 @@ package object compiletime {
2929
*/
3030
inline def error(inline msg: String): Nothing = ???
3131

32-
/** Returns the string representation of interpolated elaborated code:
33-
*
34-
* ```scala
35-
* inline def logged(p1: => Any) = {
36-
* val c = code"code: $p1"
37-
* val res = p1
38-
* (c, p1)
39-
* }
40-
* logged(identity("foo"))
41-
* // above is equivalent to:
42-
* // ("code: scala.Predef.identity("foo")", identity("foo"))
43-
* ```
44-
*
45-
* @note only by-name arguments will be displayed as "code".
46-
* Other values may display unintutively.
47-
*/
48-
transparent inline def (inline self: StringContext) code (inline args: Any*): String =
49-
${ dotty.internal.CompileTimeMacros.codeExpr('self, 'args) }
32+
extension (inline self: StringContext):
33+
/** Returns the string representation of interpolated elaborated code:
34+
*
35+
* ```scala
36+
* inline def logged(p1: => Any) = {
37+
* val c = code"code: $p1"
38+
* val res = p1
39+
* (c, p1)
40+
* }
41+
* logged(identity("foo"))
42+
* // above is equivalent to:
43+
* // ("code: scala.Predef.identity("foo")", identity("foo"))
44+
* ```
45+
*
46+
* @note only by-name arguments will be displayed as "code".
47+
* Other values may display unintutively.
48+
*/
49+
transparent inline def code (inline args: Any*): String =
50+
${ dotty.internal.CompileTimeMacros.codeExpr('self, 'args) }
51+
end extension
5052

5153
/** Same as `constValue` but returns a `None` if a constant value
5254
* cannot be constructed from the provided type. Otherwise returns

0 commit comments

Comments
 (0)