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
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/derivation.md
+17-17Lines changed: 17 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -215,15 +215,15 @@ a mirror over that array, and finally uses the `reify` method in `Reflected` to
215
215
216
216
### How to Write Generic Typeclasses
217
217
218
-
Based on the machinery developed so far it becomes possible to define type classes generically. This means that the `derived` method will compute a type class delegate for any ADT that has a `Generic`delegate, recursively.
218
+
Based on the machinery developed so far it becomes possible to define type classes generically. This means that the `derived` method will compute a type class instance for any ADT that has a given `Generic`instance, recursively.
219
219
The implementation of these methods typically uses three new type-level constructs in Dotty: inline methods, inline matches, and implicit matches. As an example, here is one possible implementation of a generic `Eql` type class, with explanations. Let's assume `Eql` is defined by the following trait:
220
220
```scala
221
221
traitEql[T] {
222
222
defeql(x: T, y: T):Boolean
223
223
}
224
224
```
225
-
We need to implement a method `Eql.derived` that produces a delegate for `Eql[T]` provided
226
-
there exists a delegate for`Generic[T]`. Here's a possible solution:
225
+
We need to implement a method `Eql.derived` that produces a given instance for `Eql[T]` provided
226
+
a given`Generic[T]`. Here's a possible solution:
227
227
```scala
228
228
inlinedefderived[T] given (ev: Generic[T]):Eql[T] =newEql[T] {
229
229
defeql(x: T, y: T):Boolean= {
@@ -239,7 +239,7 @@ there exists a delegate for `Generic[T]`. Here's a possible solution:
239
239
}
240
240
}
241
241
```
242
-
The implementation of the inline method `derived` creates a delegate for `Eql[T]` and implements its `eql` method. The right-hand side of `eql` mixes compile-time and runtime elements. In the code above, runtime elements are marked with a number in parentheses, i.e
242
+
The implementation of the inline method `derived` creates a given instance for `Eql[T]` and implements its `eql` method. The right-hand side of `eql` mixes compile-time and runtime elements. In the code above, runtime elements are marked with a number in parentheses, i.e
243
243
`(1)`, `(2)`, `(3)`. Compile-time calls that expand to runtime code are marked with a number in brackets, i.e. `[4]`, `[5]`. The implementation of `eql` consists of the following steps.
244
244
245
245
1. Map the compared values `x` and `y` to their mirrors using the `reflect` method of the implicitly passed `Generic``(1)`, `(2)`.
@@ -310,16 +310,16 @@ The last, and in a sense most interesting part of the derivation is the comparis
310
310
caseev: Eql[T] =>
311
311
ev.eql(x, y) // (15)
312
312
case _ =>
313
-
error("No `Eql` delegate was found for $T")
313
+
error("No `Eql` instance was found for $T")
314
314
}
315
315
```
316
-
`tryEql` is an inline method that takes an element type `T` and two element values of that type as arguments. It is defined using an `implicit match` that tries to find a delegate for `Eql[T]`. If a delegate`ev` is found, it proceeds by comparing the arguments using `ev.eql`. On the other hand, if no delegate is found
317
-
this signals a compilation error: the user tried a generic derivation of `Eql` for a class with an element type that does not have an `Eql`delegate itself. The error is signaled by
316
+
`tryEql` is an inline method that takes an element type `T` and two element values of that type as arguments. It is defined using an `implicit match` that tries to find a given instance for `Eql[T]`. If an instance`ev` is found, it proceeds by comparing the arguments using `ev.eql`. On the other hand, if no instance is found
317
+
this signals a compilation error: the user tried a generic derivation of `Eql` for a class with an element type that does not have an `Eql`instance itself. The error is signaled by
318
318
calling the `error` method defined in `scala.compiletime`.
319
319
320
320
**Note:** At the moment our error diagnostics for metaprogramming does not support yet interpolated string arguments for the `scala.compiletime.error` method that is called in the second case above. As an alternative, one can simply leave off the second case, then a missing typeclass would result in a "failure to reduce match" error.
321
321
322
-
**Example:** Here is a slightly polished and compacted version of the code that's generated by inline expansion for the derived `Eql`delegate for class `Tree`.
322
+
**Example:** Here is a slightly polished and compacted version of the code that's generated by inline expansion for the derived `Eql`instance for class `Tree`.
323
323
324
324
```scala
325
325
given [T] as Eql[Tree[T]] where (elemEq: Eql[T]) {
@@ -341,21 +341,21 @@ given [T] as Eql[Tree[T]] where (elemEq: Eql[T]) {
341
341
}
342
342
```
343
343
344
-
One important difference between this approach and Scala-2 typeclass derivation frameworks such as Shapeless or Magnolia is that no automatic attempt is made to generate typeclass delegates for elements recursively using the generic derivation framework. There must be a delegate for `Eql[T]` (which can of course be produced in turn using `Eql.derived`), or the compilation will fail. The advantage of this more restrictive approach to typeclass derivation is that it avoids uncontrolled transitive typeclass derivation by design. This keeps code sizes smaller, compile times lower, and is generally more predictable.
344
+
One important difference between this approach and Scala-2 typeclass derivation frameworks such as Shapeless or Magnolia is that no automatic attempt is made to generate typeclass instances for elements recursively using the generic derivation framework. There must be a given instance for `Eql[T]` (which can of course be produced in turn using `Eql.derived`), or the compilation will fail. The advantage of this more restrictive approach to typeclass derivation is that it avoids uncontrolled transitive typeclass derivation by design. This keeps code sizes smaller, compile times lower, and is generally more predictable.
345
345
346
-
### Deriving Delegates Elsewhere
346
+
### Deriving Instances Elsewhere
347
347
348
-
Sometimes one would like to derive a typeclass delegate for an ADT after the ADT is defined, without being able to change the code of the ADT itself.
349
-
To do this, simply define a delegate with the `derived` method of the typeclass as right-hand side. E.g, to implement `Ordering` for `Option`, define:
348
+
Sometimes one would like to derive a typeclass instance for an ADT after the ADT is defined, without being able to change the code of the ADT itself.
349
+
To do this, simply define an instance with the `derived` method of the typeclass as right-hand side. E.g, to implement `Ordering` for `Option`, define:
Copy file name to clipboardExpand all lines: docs/docs/reference/features-classification.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ Since these are additions, there's generally no migration cost for old code. An
36
36
These constructs replace existing constructs with the aim of making the language safer and simpler to use, and to promote uniformity in code style.
37
37
38
38
-[Trait Parameters](other-new-features/trait-parameters.md) replace [early initializers](dropped-features/early-initializers.md) with a more generally useful construct.
39
-
-[Delegates](contextual/delegates.md)
39
+
-[Given Instances](contextual/delegates.md)
40
40
replace implicit objects and defs, focussing on intent over mechanism.
41
41
-[Given Clauses](contextual/given-clauses.md) replace implicit parameters, avoiding their ambiguities.
42
42
-[Extension Methods](contextual/extension-methods.md) replace implicit classes with a clearer and simpler mechanism.
@@ -71,7 +71,7 @@ For the next several versions, old features will remain available and deprecatio
71
71
These constructs are restricted to make the language safer.
72
72
73
73
-[Implicit Conversions](contextual/conversions.md): there is only one way to define implicit conversions instead of many, and potentially surprising implicit conversions require a language import.
74
-
-[Delegate Imports](contextual/import-delegate.md): implicits now require a special form of import, to make the import clearly visible.
74
+
-[Given Imports](contextual/import-delegate.md): implicits now require a special form of import, to make the import clearly visible.
75
75
-[Type Projection](dropped-features/type-projection.md): only classes can be used as prefix `C` of a type projection `C#A`. Type projection on abstract types is no longer supported since it is unsound.
76
76
-[Multiversal Equality](contextual/multiversal-equality.md) implements an "opt-in" scheme to rule out nonsensical comparisons with `==` and `!=`.
77
77
-[@infix and @alpha](https://github.com/lampepfl/dotty/pull/5975)
Copy file name to clipboardExpand all lines: docs/docs/reference/other-new-features/export.md
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ They can be accessed inside `Copier` as well as from outside:
43
43
An export clause has the same format as an import clause. Its general form is:
44
44
```scala
45
45
exportpath . { sel_1, ..., sel_n }
46
-
exportdelegatepath . { sel_1, ..., sel_n }
46
+
exportgivenpath . { sel_1, ..., sel_n }
47
47
```
48
48
It consists of a qualifier expression `path`, which must be a stable identifier, followed by
49
49
one or more selectors `sel_i` that identify what gets an alias. Selectors can be
@@ -61,14 +61,14 @@ A member is _eligible_ if all of the following holds:
61
61
- its owner is not a base class of the class(*) containing the export clause,
62
62
- it is accessible at the export clause,
63
63
- it is not a constructor, nor the (synthetic) class part of an object,
64
-
- it is delegate (or an old-style `implicit` value)
65
-
if and only if the export is tagged with `delegate`.
64
+
- it is a given instance (or an old-style `implicit` value)
65
+
if and only if the export is tagged with `given`.
66
66
67
67
It is a compile-time error if a simple or renaming selector does not identify any eligible
68
68
members.
69
69
70
70
Type members are aliased by type definitions, and term members are aliased by method definitions. Export aliases copy the type and value parameters of the members they refer to.
71
-
Export aliases are always `final`. Aliases of delegates are again defines as delegates (and aliases of old-style implicits are `implicit`). There are no other modifiers that can be given to an alias. This has the following consequences for overriding:
71
+
Export aliases are always `final`. Aliases of given instances are again defined as givens (and aliases of old-style implicits are `implicit`). There are no other modifiers that can be given to an alias. This has the following consequences for overriding:
72
72
73
73
- Export aliases cannot be overridden, since they are final.
74
74
- Export aliases cannot override concrete members in base classes, since they are
Copy file name to clipboardExpand all lines: docs/docs/reference/overview.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ These new constructs directly model core features of DOT, higher-kinded types, a
33
33
These constructs replace existing constructs with the aim of making the language safer and simpler to use, and to promote uniformity in code style.
34
34
35
35
-[Trait Parameters](other-new-features/trait-parameters.md) replace [early initializers](dropped-features/early-initializers.md) with a more generally useful construct.
36
-
-[Delegates](contextual/delegates.md)
36
+
-[Given Instances](contextual/delegates.md)
37
37
replace implicit objects and defs, focussing on intent over mechanism.
38
38
-[Given Clauses](contextual/given-clauses.md) replace implicit parameters, avoiding their ambiguities.
39
39
-[Extension Methods](contextual/extension-methods.md) replace implicit classes with a clearer and simpler mechanism.
@@ -58,7 +58,7 @@ e a special case. There are currently no deprecation plans for value classes, si
58
58
These constructs are restricted to make the language safer.
59
59
60
60
-[Implicit Conversions](contextual/conversions.md): there is only one way to define implicit conversions instead of many, and potentially surprising implicit conversions require a language import.
61
-
-[Delegate Imports](contextual/import-delegate.md): implicits now require a special form of import, to make the import clearly visible.
61
+
-[Given Imports](contextual/import-delegate.md): implicits now require a special form of import, to make the import clearly visible.
62
62
-[Type Projection](dropped-features/type-projection.md): only classes can be used as prefix `C` of a type projection `C#A`. Type projection on abstract types is no longer supported since it is unsound.
63
63
-[Multiversal Equality](contextual/multiversal-equality.md) implements an "opt-in" scheme to rule out nonsensical comparisons with `==` and `!=`.
64
64
-[@infix and @alpha](https://github.com/lampepfl/dotty/pull/5975)
0 commit comments