Skip to content

Add the method #5893

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 1 commit into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/reference/contextual/derivation.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ calling the `error` method defined in `scala.compiletime`.
```scala
implied [T] with (elemEq: Eq[T]) for Eq[Tree[T]] {
def eql(x: Tree[T], y: Tree[T]): Boolean = {
val ev = infer[Generic[Tree[T]]]
val ev = the[Generic[Tree[T]]]
val mx = ev.reflect(x)
val my = ev.reflect(y)
mx.ordinal == my.ordinal && {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ implied optionCodec[T] given (ev: => Codec[T]) for Codec[Option[T]] {
}
}

val s = infer[Codec[Option[Int]]]
val s = the[Codec[Option[Int]]]

s.write(Some(33))
s.write(None)
Expand Down Expand Up @@ -54,7 +54,7 @@ The precise steps for constructing an inferable argument for a by-name parameter
In the example above, the definition of `s` would be expanded as follows.

```scala
val s = infer[Test.Codec[Option[Int]]](
val s = the[Test.Codec[Option[Int]]](
optionCodec[Int](intCodec))
```

Expand Down
10 changes: 5 additions & 5 deletions docs/docs/reference/contextual/inferable-params.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ f("abc") given ctx

## Querying Implied Instances

A method `infer` in `Predef` creates an implied instance of a given type. For example,
A method `the` in `Predef` creates an implied instance of a given type. For example,
the implied instance of `Ord[List[Int]]` is generated by
```scala
infer[Ord[List[Int]]] // reduces to ListOrd given IntOrd
the[Ord[List[Int]]] // reduces to ListOrd given IntOrd
```
The `infer` method is simply defined as the identity function over an inferable parameter.
The `the` method is simply defined as the (non-widening) identity function over an inferable parameter.
```scala
def infer[T] given (x: T) = x
def the[T] given (x: T): x.type = x
```
Functions like `infer` that have only inferable parameters are also called _context queries_.
Functions like `the` that have only inferable parameters are also called _context queries_.

## Syntax

Expand Down
6 changes: 5 additions & 1 deletion docs/docs/reference/contextual/relationship-implicits.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ Explicit arguments to inferable parameters _must_ be written using `given`,
mirroring the definition syntax. E.g, `max(2, 3) given IntOrd`.
Scala 2 uses normal applications `max(2, 3)(IntOrd)` instead. The Scala 2 syntax has some inherent ambiguities and restrictions which are overcome by the new syntax. For instance, multiple implicit parameter lists are not available in the old syntax, even though they can be simulated using auxiliary objects in the "Aux" pattern.

The `infer` method corresponds to `implicitly` in Scala 2.
The `the` method corresponds to `implicitly` in Scala 2.
It is precisely the same as the `the` method in Shapeless.
The difference between `the` (in both versions) and `implicitly` is
that `the` can return a more precise type than the type that was
asked for.

### Context Bounds

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/reference/contextual/typeclasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait Monoid[T] extends SemiGroup[T] {
def unit: T
}
object Monoid {
def apply[T] = infer[Monoid[T]]
def apply[T] = the[Monoid[T]]
}

implied for Monoid[String] {
Expand Down
38 changes: 38 additions & 0 deletions library/src-bootstrapped/dotty/DottyPredef.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dotty

object DottyPredef {

@forceInline final def assert(assertion: => Boolean, message: => Any): Unit = {
if (!assertion)
assertFail(message)
}

@forceInline final def assert(assertion: => Boolean): Unit = {
if (!assertion)
assertFail()
}

def assertFail(): Unit = throw new java.lang.AssertionError("assertion failed")
def assertFail(message: => Any): Unit = throw new java.lang.AssertionError("assertion failed: " + message)

@forceInline final def implicitly[T](implicit ev: T): T = ev

@forceInline def locally[T](body: => T): T = body

/**
* Retrieve the single value of a type with a unique inhabitant.
*
* @example {{{
* object Foo
* val foo = valueOf[Foo.type]
* // foo is Foo.type = Foo
*
* val bar = valueOf[23]
* // bar is 23.type = 23
* }}}
* @group utilities
*/
@forceInline def valueOf[T](implicit vt: ValueOf[T]): T = vt.value

inline def the[T](implicit x: T): x.type = x
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import scala.forceInline

object DottyPredef {


@forceInline final def assert(assertion: => Boolean, message: => Any): Unit = {
if (!assertion)
assertFail(message)
Expand Down Expand Up @@ -36,6 +35,4 @@ object DottyPredef {
* @group utilities
*/
@forceInline def valueOf[T](implicit vt: ValueOf[T]): T = vt.value

@forceInline def infer[T](implicit x: T) = x
}
10 changes: 5 additions & 5 deletions tests/pos/reference/instances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,20 @@ object Instances extends Common {
def f() = {
locally {
implied for Context = this.ctx
println(infer[Context].value)
println(the[Context].value)
}
locally {
lazy val ctx1 = this.ctx
implied for Context = ctx1
println(infer[Context].value)
println(the[Context].value)
}
locally {
implied d[T] for D[T]
println(infer[D[Int]])
println(the[D[Int]])
}
locally {
implied given Context for D[Int]
println(infer[D[Int]])
println(the[D[Int]])
}
}
}
Expand Down Expand Up @@ -205,7 +205,7 @@ object AnonymousInstances extends Common {
}

def sum[T: Monoid](xs: List[T]): T =
xs.foldLeft(infer[Monoid[T]].unit)(_.combine(_))
xs.foldLeft(the[Monoid[T]].unit)(_.combine(_))
}

object Implicits extends Common {
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/the.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
object Test {

trait Foo { type T; val x: T }

implied intFoo for Foo {
type T = Int
val x = 3
}

val y: Int = the[Foo].x

}
4 changes: 2 additions & 2 deletions tests/run/implicit-disambiguation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class C extends A {
}
object M {
def f given B, C : String = {
implied a for A = infer[B]
infer[A].show
implied a for A = the[B]
the[A].show
}
}
object Test extends App {
Expand Down