-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve implicit conversion docs and some more tests #6098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68e2c9f
Show and test implicit conversions using SAM types.
odersky 44c1ec1
Reformulate core/Decorators.scala with extension methods
odersky c11a3fd
Example from latest Cochis paper
odersky 70dca88
Test case for #5966
odersky 5625ca8
Fix typo
odersky 4c5c3ff
Update docs/docs/reference/contextual/conversions.md
smarter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
package dotty.tools.dotc | ||
package core | ||
|
||
import annotation.tailrec | ||
import Symbols._ | ||
import Contexts._, Names._, Phases._, printing.Texts._, printing.Printer | ||
import util.Spans.Span, util.SourcePosition | ||
import collection.mutable.ListBuffer | ||
import dotty.tools.dotc.transform.MegaPhase | ||
import ast.tpd._ | ||
import scala.language.implicitConversions | ||
import printing.Formatting._ | ||
|
||
/** This object provides useful conversions and extension methods for types defined elsewhere */ | ||
object DecoratorsX { | ||
|
||
/** Turns Strings into PreNames, adding toType/TermName methods */ | ||
class StringPreName(s: String) extends AnyVal with PreName { | ||
def toTypeName: TypeName = typeName(s) | ||
def toTermName: TermName = termName(s) | ||
def toText(printer: Printer): Text = Str(s) | ||
} | ||
implied for Conversion[String, StringPreName] = new StringPreName(_) | ||
|
||
final val MaxFilterRecursions = 1000 | ||
|
||
implied { | ||
def (s: String) splitWhere (f: Char => Boolean, doDropIndex: Boolean): Option[(String, String)] = { | ||
def splitAt(idx: Int, doDropIndex: Boolean): Option[(String, String)] = | ||
if (idx == -1) None | ||
else Some((s.take(idx), s.drop(if (doDropIndex) idx + 1 else idx))) | ||
|
||
splitAt(s.indexWhere(f), doDropIndex) | ||
} | ||
|
||
/** Implements a findSymbol method on iterators of Symbols that | ||
* works like find but avoids Option, replacing None with NoSymbol. | ||
*/ | ||
def (it: Iterator[Symbol]) findSymbol(p: Symbol => Boolean): Symbol = { | ||
while (it.hasNext) { | ||
val sym = it.next() | ||
if (p(sym)) return sym | ||
} | ||
NoSymbol | ||
} | ||
|
||
def (xs: List[T]) mapconserve [T, U] (f: T => U): List[U] = { | ||
@tailrec | ||
def loop(mapped: ListBuffer[U], unchanged: List[U], pending: List[T]): List[U] = | ||
if (pending.isEmpty) { | ||
if (mapped eq null) unchanged | ||
else mapped.prependToList(unchanged) | ||
} else { | ||
val head0 = pending.head | ||
val head1 = f(head0) | ||
|
||
if (head1.asInstanceOf[AnyRef] eq head0.asInstanceOf[AnyRef]) | ||
loop(mapped, unchanged, pending.tail) | ||
else { | ||
val b = if (mapped eq null) new ListBuffer[U] else mapped | ||
var xc = unchanged | ||
while (xc ne pending) { | ||
b += xc.head | ||
xc = xc.tail | ||
} | ||
b += head1 | ||
val tail0 = pending.tail | ||
loop(b, tail0.asInstanceOf[List[U]], tail0) | ||
} | ||
} | ||
loop(null, xs.asInstanceOf[List[U]], xs) | ||
} | ||
|
||
/** Like `xs filter p` but returns list `xs` itself - instead of a copy - | ||
* if `p` is true for all elements and `xs` is not longer | ||
* than `MaxFilterRecursions`. | ||
*/ | ||
def (xs: List[T]) filterConserve [T] (p: T => Boolean): List[T] = { | ||
def loop(xs: List[T], nrec: Int): List[T] = xs match { | ||
case Nil => xs | ||
case x :: xs1 => | ||
if (nrec < MaxFilterRecursions) { | ||
val ys1 = loop(xs1, nrec + 1) | ||
if (p(x)) | ||
if (ys1 eq xs1) xs else x :: ys1 | ||
else | ||
ys1 | ||
} else xs filter p | ||
} | ||
loop(xs, 0) | ||
} | ||
|
||
/** Like `(xs, ys).zipped.map(f)`, but returns list `xs` itself | ||
* - instead of a copy - if function `f` maps all elements of | ||
* `xs` to themselves. Also, it is required that `ys` is at least | ||
* as long as `xs`. | ||
*/ | ||
def (xs: List[T]) zipWithConserve [T, U] (ys: List[U])(f: (T, U) => T): List[T] = | ||
if (xs.isEmpty || ys.isEmpty) Nil | ||
else { | ||
val x1 = f(xs.head, ys.head) | ||
val xs1 = xs.tail.zipWithConserve(ys.tail)(f) | ||
if ((x1.asInstanceOf[AnyRef] eq xs.head.asInstanceOf[AnyRef]) && | ||
(xs1 eq xs.tail)) xs | ||
else x1 :: xs1 | ||
} | ||
|
||
def (xs: List[T]) hasSameLengthAs [T, U] (ys: List[U]): Boolean = { | ||
@tailrec def loop(xs: List[T], ys: List[U]): Boolean = | ||
if (xs.isEmpty) ys.isEmpty | ||
else ys.nonEmpty && loop(xs.tail, ys.tail) | ||
loop(xs, ys) | ||
} | ||
|
||
@tailrec | ||
def (xs: List[T]) eqElements [T] (ys: List[AnyRef]): Boolean = xs match { | ||
case x :: _ => | ||
ys match { | ||
case y :: _ => | ||
x.asInstanceOf[AnyRef].eq(y) && | ||
xs.tail.eqElements(ys.tail) | ||
case _ => false | ||
} | ||
case nil => ys.isEmpty | ||
} | ||
|
||
def (xs: List[T]) | [T] (ys: List[T]): List[T] = xs ::: (ys filterNot (xs contains _)) | ||
|
||
/** Intersection on lists seen as sets */ | ||
def (xs: List[T]) & [T] (ys: List[T]): List[T] = xs filter (ys contains _) | ||
|
||
def (xss: List[List[T]]) nestedMap [T, U] (f: T => U): List[List[U]] = xss map (_ map f) | ||
def (xss: List[List[T]]) nestedMapconserve [T, U](f: T => U): List[List[U]] = xss mapconserve (_ mapconserve f) | ||
|
||
def (text: Text) show given (ctx: Context): String = | ||
text.mkString(ctx.settings.pageWidth.value, ctx.settings.printLines.value) | ||
|
||
/** Test whether a list of strings representing phases contains | ||
* a given phase. See [[config.CompilerCommand#explainAdvanced]] for the | ||
* exact meaning of "contains" here. | ||
*/ | ||
def (names: List[String]) containsPhase (phase: Phase): Boolean = | ||
names.nonEmpty && { | ||
phase match { | ||
case phase: MegaPhase => phase.miniPhases.exists(names.containsPhase(_)) | ||
case _ => | ||
names exists { name => | ||
name == "all" || { | ||
val strippedName = name.stripSuffix("+") | ||
val logNextPhase = name != strippedName | ||
phase.phaseName.startsWith(strippedName) || | ||
(logNextPhase && phase.prev.phaseName.startsWith(strippedName)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
def (x: T) reporting [T] (op: T => String, printer: config.Printers.Printer = config.Printers.default): T = { | ||
printer.println(op(x)) | ||
x | ||
} | ||
|
||
def (x: T) assertingErrorsReported [T] given Context: T = { | ||
assert(the[Context].reporter.errorsReported) | ||
x | ||
} | ||
|
||
def (x: T) assertingErrorsReported [T] (msg: => String) given Context: T = { | ||
assert(the[Context].reporter.errorsReported, msg) | ||
x | ||
} | ||
|
||
/** General purpose string formatting */ | ||
def (sc: StringContext) i (args: Any*) given Context: String = | ||
new StringFormatter(sc).assemble(args) | ||
|
||
/** Formatting for error messages: Like `i` but suppress follow-on | ||
* error messages after the first one if some of their arguments are "non-sensical". | ||
*/ | ||
def (sc: StringContext) em (args: Any*) given Context: String = | ||
new ErrorMessageFormatter(sc).assemble(args) | ||
|
||
/** Formatting with added explanations: Like `em`, but add explanations to | ||
* give more info about type variables and to disambiguate where needed. | ||
*/ | ||
def (sc: StringContext) ex (args: Any*) given Context: String = | ||
explained(sc.em(args: _*)) | ||
|
||
/** Formatter that adds syntax highlighting to all interpolated values */ | ||
def (sc: StringContext) hl (args: Any*) given Context: String = | ||
new SyntaxFormatter(sc).assemble(args).stripMargin | ||
|
||
def (arr: Array[T]) binarySearch [T <: AnyRef] (x: T): Int = | ||
java.util.Arrays.binarySearch(arr.asInstanceOf[Array[Object]], x) | ||
} | ||
|
||
/** Entrypoint for explanation string interpolator: | ||
* | ||
* ``` | ||
* ex"disambiguate $tpe1 and $tpe2" | ||
* ``` | ||
*/ | ||
def explained(op: given Context => String) given Context: String = | ||
printing.Formatting.explained(ctx => op given ctx) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
object Test { | ||
def foo = given (v: Int) => (x: Int) => v + x | ||
implied myInt for Int = 4 | ||
|
||
foo.apply(1) | ||
foo given 2 | ||
foo(3) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
import Predef.{$conforms => _} | ||
trait A { | ||
implied id[X] for (X => X) = x => x | ||
def trans[X](x: X) given (f: X => X) = f(x) // (2) | ||
} | ||
object Test extends A with App{ | ||
implied succ for (Int => Int) = x => x + 1 // (3) | ||
def bad[X](x: X): X = trans[X](x) // (4) unstable definition ! | ||
val v1 = bad [Int] (3) // (5) evaluates to 3 | ||
assert(v1 == 3) | ||
val v2 = trans [Int] (3) | ||
assert(v2 == 4) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.