Skip to content

Commit 89f42f4

Browse files
Merge pull request #9407 from dotty-staging/use-extension-methods
Use extension methods
2 parents fd18546 + 7f454a1 commit 89f42f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+83
-98
lines changed

compiler/src/dotty/tools/dotc/CompilationUnit.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import tpd.{Tree, TreeTraverser}
1212
import typer.PrepareInlineable.InlineAccessors
1313
import typer.Nullables
1414
import transform.SymUtils._
15-
import core.Decorators.{given _}
15+
import core.Decorators._
1616
import config.SourceVersion
1717

1818
class CompilationUnit protected (val source: SourceFile) {

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package ast
55
import core._
66
import util.Spans._, Types._, Contexts._, Constants._, Names._, NameOps._, Flags._
77
import Symbols._, StdNames._, Trees._, Phases._, ContextOps._
8-
import Decorators.{given _}, transform.SymUtils._
8+
import Decorators._, transform.SymUtils._
99
import NameKinds.{UniqueName, EvidenceParamName, DefaultGetterName}
1010
import typer.{FrontEnd, Namer}
1111
import util.{Property, SourceFile, SourcePosition}

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
831831
/** Structural tree comparison (since == on trees is reference equality).
832832
* For the moment, only Ident, Select, Literal, Apply and TypeApply are supported
833833
*/
834-
implicit class StructuralEqDeco(t1: Tree) {
834+
extension (t1: Tree) {
835835
def === (t2: Tree)(using Context): Boolean = (t1, t2) match {
836836
case (t1: Ident, t2: Ident) =>
837837
t1.symbol == t2.symbol

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import transform.TypeUtils._
99
import core._
1010
import util.Spans._, Types._, Contexts._, Constants._, Names._, Flags._, NameOps._
1111
import Symbols._, StdNames._, Annotations._, Trees._, Symbols._
12-
import Decorators.{given _}, DenotTransformers._
12+
import Decorators._, DenotTransformers._
1313
import collection.{immutable, mutable}
1414
import util.{Property, SourceFile, NoSource}
1515
import NameKinds.{TempResultName, OuterSelectName}
@@ -1111,12 +1111,11 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11111111
!(sym.is(Method) && sym.info.isInstanceOf[MethodOrPoly]) // if is a method it is parameterless
11121112
}
11131113

1114-
implicit class ListOfTreeDecorator(val xs: List[tpd.Tree]) extends AnyVal {
1114+
extension (xs: List[tpd.Tree]):
11151115
def tpes: List[Type] = xs match {
11161116
case x :: xs1 => x.tpe :: xs1.tpes
11171117
case nil => Nil
11181118
}
1119-
}
11201119

11211120
/** A trait for loaders that compute trees. Currently implemented just by DottyUnpickler. */
11221121
trait TreeProvider {

compiler/src/dotty/tools/dotc/classpath/ClassPathFactory.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package dotty.tools.dotc.classpath
55

66
import dotty.tools.io.{AbstractFile, VirtualDirectory}
7-
import FileUtils.AbstractFileOps
7+
import FileUtils._
88
import dotty.tools.io.ClassPath
99
import dotty.tools.dotc.core.Contexts._
1010

compiler/src/dotty/tools/dotc/classpath/FileUtils.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import dotty.tools.io.AbstractFile
1212
* Common methods related to Java files and abstract files used in the context of classpath
1313
*/
1414
object FileUtils {
15-
implicit class AbstractFileOps(val file: AbstractFile) extends AnyVal {
15+
extension (file: AbstractFile) {
1616
def isPackage: Boolean = file.isDirectory && mayBeValidPackage(file.name)
1717

1818
def isClass: Boolean = !file.isDirectory && file.hasExtension("class") && !file.name.endsWith("$class.class")
@@ -30,7 +30,7 @@ object FileUtils {
3030
def toURLs(default: => Seq[URL] = Seq.empty): Seq[URL] = if (file.file == null) default else Seq(file.toURL)
3131
}
3232

33-
implicit class FileOps(val file: JFile) extends AnyVal {
33+
extension (file: JFile) {
3434
def isPackage: Boolean = file.isDirectory && mayBeValidPackage(file.getName)
3535

3636
def isClass: Boolean = file.isFile && file.getName.endsWith(".class") && !file.getName.endsWith("$class.class")

compiler/src/dotty/tools/dotc/classpath/ZipArchiveFileLookup.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import java.io.File
77
import java.net.URL
88

99
import dotty.tools.io.{ AbstractFile, FileZipArchive }
10-
import FileUtils.AbstractFileOps
10+
import FileUtils._
1111
import dotty.tools.io.{ClassPath, ClassRepresentation}
1212

1313
/**

compiler/src/dotty/tools/dotc/config/Settings.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ object Settings {
184184
}
185185

186186
object Setting {
187-
implicit class SettingDecorator[T](val setting: Setting[T]) extends AnyVal {
187+
extension [T](setting: Setting[T]) {
188188
def value(using Context): T = setting.valueIn(ctx.settingsState)
189189
def update(x: T)(using Context): SettingsState = setting.updateIn(ctx.settingsState, x)
190190
def isDefault(using Context): Boolean = setting.isDefaultIn(ctx.settingsState)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,15 +678,15 @@ object Contexts {
678678
end ops
679679

680680
// TODO: Fix issue when converting ModeChanges and FreshModeChanges to extension givens
681-
implicit class ModeChanges(val c: Context) extends AnyVal {
681+
extension (c: Context) {
682682
final def withModeBits(mode: Mode): Context =
683683
if (mode != c.mode) c.fresh.setMode(mode) else c
684684

685685
final def addMode(mode: Mode): Context = withModeBits(c.mode | mode)
686686
final def retractMode(mode: Mode): Context = withModeBits(c.mode &~ mode)
687687
}
688688

689-
implicit class FreshModeChanges(val c: FreshContext) extends AnyVal {
689+
extension (c: FreshContext) {
690690
final def addMode(mode: Mode): c.type = c.setMode(c.mode | mode)
691691
final def retractMode(mode: Mode): c.type = c.setMode(c.mode &~ mode)
692692
}

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,26 @@ object Decorators {
2828
case s: String => termName(s)
2929
case n: Name => n.toTermName
3030

31-
implicit class StringDecorator(val s: String) extends AnyVal {
31+
extension (s: String):
3232
def splitWhere(f: Char => Boolean, doDropIndex: Boolean): Option[(String, String)] = {
3333
def splitAt(idx: Int, doDropIndex: Boolean): Option[(String, String)] =
3434
if (idx == -1) None
3535
else Some((s.take(idx), s.drop(if (doDropIndex) idx + 1 else idx)))
3636

3737
splitAt(s.indexWhere(f), doDropIndex)
3838
}
39-
}
4039

4140
/** Implements a findSymbol method on iterators of Symbols that
4241
* works like find but avoids Option, replacing None with NoSymbol.
4342
*/
44-
implicit class SymbolIteratorDecorator(val it: Iterator[Symbol]) extends AnyVal {
43+
extension (it: Iterator[Symbol]):
4544
final def findSymbol(p: Symbol => Boolean): Symbol = {
4645
while (it.hasNext) {
4746
val sym = it.next()
4847
if (p(sym)) return sym
4948
}
5049
NoSymbol
5150
}
52-
}
5351

5452
final val MaxFilterRecursions = 1000
5553

@@ -161,29 +159,27 @@ object Decorators {
161159
def & (ys: List[T]): List[T] = xs filter (ys contains _)
162160
}
163161

164-
given ListOfListDecorator as AnyRef:
165-
extension [T, U](xss: List[List[T]]):
166-
def nestedMap(f: T => U): List[List[U]] =
167-
xss.map(_.map(f))
168-
def nestedMapConserve(f: T => U): List[List[U]] =
169-
xss.mapconserve(_.mapconserve(f))
170-
def nestedZipWithConserve(yss: List[List[U]])(f: (T, U) => T): List[List[T]] =
171-
xss.zipWithConserve(yss)((xs, ys) => xs.zipWithConserve(ys)(f))
172-
end extension
173-
174-
implicit class TextToString(val text: Text) extends AnyVal {
162+
extension [T, U](xss: List[List[T]]):
163+
def nestedMap(f: T => U): List[List[U]] =
164+
xss.map(_.map(f))
165+
def nestedMapConserve(f: T => U): List[List[U]] =
166+
xss.mapconserve(_.mapconserve(f))
167+
def nestedZipWithConserve(yss: List[List[U]])(f: (T, U) => T): List[List[T]] =
168+
xss.zipWithConserve(yss)((xs, ys) => xs.zipWithConserve(ys)(f))
169+
end extension
170+
171+
extension (text: Text):
175172
def show(using Context): String = text.mkString(ctx.settings.pageWidth.value, ctx.settings.printLines.value)
176-
}
177173

178174
/** Test whether a list of strings representing phases contains
179175
* a given phase. See [[config.CompilerCommand#explainAdvanced]] for the
180176
* exact meaning of "contains" here.
181177
*/
182-
implicit class PhaseListDecorator(val names: List[String]) extends AnyVal {
178+
extension (names: List[String]) {
183179
def containsPhase(phase: Phase): Boolean =
184180
names.nonEmpty && {
185181
phase match {
186-
case phase: MegaPhase => phase.miniPhases.exists(containsPhase)
182+
case phase: MegaPhase => phase.miniPhases.exists(x => names.containsPhase(x))
187183
case _ =>
188184
names exists { name =>
189185
name == "all" || {
@@ -197,7 +193,7 @@ object Decorators {
197193
}
198194
}
199195

200-
implicit class reportDeco[T](x: T) extends AnyVal {
196+
extension [T](x: T) {
201197
def reporting(
202198
op: WrappedResult[T] ?=> String,
203199
printer: config.Printers.Printer = config.Printers.default): T = {
@@ -206,7 +202,7 @@ object Decorators {
206202
}
207203
}
208204

209-
implicit class genericDeco[T](val x: T) extends AnyVal {
205+
extension [T](x: T) {
210206
def assertingErrorsReported(using Context): T = {
211207
assert(ctx.reporter.errorsReported)
212208
x
@@ -217,7 +213,7 @@ object Decorators {
217213
}
218214
}
219215

220-
implicit class StringInterpolators(val sc: StringContext) extends AnyVal {
216+
extension (sc: StringContext) {
221217
/** General purpose string formatting */
222218
def i(args: Any*)(using Context): String =
223219
new StringFormatter(sc).assemble(args)
@@ -235,8 +231,8 @@ object Decorators {
235231
explained(em(args: _*))
236232
}
237233

238-
implicit class ArrayInterpolator[T <: AnyRef](val arr: Array[T]) extends AnyVal {
234+
extension [T <: AnyRef](arr: Array[T]):
239235
def binarySearch(x: T): Int = java.util.Arrays.binarySearch(arr.asInstanceOf[Array[Object]], x)
240-
}
236+
241237
}
242238

0 commit comments

Comments
 (0)