You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Replaces "instance as a modifier scheme". Advantage: Simpler, fewer
possible feture interactions. Disadvantage: Less straightforward to
convert existsing implicits to new scheme.
Copy file name to clipboardExpand all lines: docs/docs/reference/instances/context-params.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,6 +68,14 @@ f("abc") with ctx
68
68
Context parameters may be given either as a normal parameter list `(...)`
69
69
or as a sequence of types. To distinguish the two, a leading `(` always indicates a parameter list.
70
70
71
+
## Summoning an Instance
72
+
73
+
A method `summon` in `Predef` creates an implicit instance value for a given type, analogously to what `implicitly[T]` did. The only difference between the two is that
74
+
`summon` takes a context parameter, where `implicitly` took an old-style implicit parameter:
75
+
```scala
76
+
defsummon[T] with (x: T) = x
77
+
```
78
+
71
79
## Syntax
72
80
73
81
Here is the new syntax of parameters and arguments seen as a delta from the [standard context free syntax of Scala 3](http://dotty.epfl.ch/docs/internals/syntax.html).
An alias instance creates an instance that is equal to some expression. E.g.,
167
+
```
168
+
instance ctx of ExecutionContext = currentThreadPool().context
169
+
```
170
+
Here, we create an instance `ctx` of type `ExecutionContext` that resolves to the
171
+
right hand side `currentThreadPool().context`. Each time an instance of `ExecutionContext`
172
+
is demanded, the result of evaluating the right-hand side expression is returned. The instance definition is equivalent to the following implicit definition:
173
+
```
174
+
final implicit def ctx: ExecutionContext = currentThreadPool().context
175
+
```
176
+
Alias instances may be anonymous, e.g.
177
+
```
178
+
instance of Position = enclosingTree.position
179
+
```
180
+
An alias instance can have type and context parameters just like any other instance definition, but it can only implement a single type.
164
181
165
182
## Syntax
166
183
167
184
Here is the new syntax of instance definitions, seen as a delta from the [standard context free syntax of Scala 3](http://dotty.epfl.ch/docs/internals/syntax.html).
The identifier `id` can be omitted only if either the `of` part or the template body is present. If the `of` part is missing, the template body must define at least one extension method.
195
+
The identifier `id` can be omitted only if either the `of` part or the template body is present. If the `of` part is missing, the template body must define at least one extension method.
Copy file name to clipboardExpand all lines: docs/docs/reference/instances/replacing-implicits.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,25 +9,26 @@ These idioms can by-and-large be mapped to existing implicits. The only exceptio
9
9
10
10
The contents of this page are more tentative than the ones of the previous pages. The concepts described in the previous pages are useful independently whether the changes on this page are adopted.
11
11
12
-
The current Dotty implementation implements the new concepts described on this page (implicit as a modifier and the summon method), but it does not remove any of the old-style implicit constructs. It cannot do this since support
12
+
The current Dotty implementation implements the new concepts described on this page (alias instances and the summon method), but it does not remove any of the old-style implicit constructs. It cannot do this since support
13
13
for old-style implicits is an essential part of the common language subset of Scala 2 and Scala 3.0. Any deprecation and subsequent removal of these constructs would have to come later, in a version following 3.0. The `implicit` modifier can be removed from the language at the end of this development, if it happens.
14
14
15
-
## `instance` As A Modifier.
15
+
## Alias Instances
16
16
17
-
`instance` can be used as a modifier for `val` and `def` definitions. Examples:
18
-
```scala
19
-
instance valsymDeco:SymDeco
20
-
instance valsymDeco:SymDeco= compilerSymOps
21
-
instance valctx= localCtx
22
-
instance deff[T]:C[T] =newC[T]
23
-
instance defgwith (ctx: Context):D=newD(ctx)
17
+
An alias instance creates an instance that is equal to some expression.
24
18
```
25
-
The `instance` modifier must be followed directly by `val` or `def`; no other intervening modifiers are permitted.
26
-
When used as a modifier, `instance` generally has the same meaning as the current `implicit` modifier, with the following exceptions:
27
-
28
-
1.`instance def` definitions can only have context parameters in `with` clauses. Old style `implicit` parameters are not supported.
29
-
2.`instance` cannot be used to define an implicit conversion or an implicit class.
30
-
19
+
instance ctx of ExecutionContext = currentThreadPool().context
20
+
```
21
+
Here, we create an instance `ctx` of type `ExecutionContext` that resolves to the
22
+
right hand side `currentThreadPool().context`. Each time an instance of `ExecutionContext`
23
+
is demanded, the result of evaluating the right-hand side expression is returned. The instance definition is equivalent to the following implicit definition:
24
+
```
25
+
final implicit def ctx: ExecutionContext = currentThreadPool().context
26
+
```
27
+
Alias instances may be anonymous, e.g.
28
+
```
29
+
instance of Position = enclosingTree.position
30
+
```
31
+
An alias instance can have type and context parameters just like any other instance definition, but it can only implement a single type.
31
32
32
33
## Replaced: Implicit Conversions
33
34
@@ -59,9 +60,8 @@ def summon[T] with (x: T) = x
59
60
60
61
The syntax changes for this page are summarized as follows:
61
62
```
62
-
InstanceDef ::= ...
63
-
| ‘val’ PatDef
64
-
| ‘def’ MethodDef
63
+
InstanceBody ::= ...
64
+
| ‘of’ Type ‘=’ Expr
65
65
```
66
66
In addition, the `implicit` modifier is removed together with all [productions]((http://dotty.epfl.ch/docs/internals/syntax.html) that reference it.
0 commit comments