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/motivation.md
+14-13Lines changed: 14 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -5,12 +5,12 @@ title: "Overview"
5
5
6
6
### Critique of the Status Quo
7
7
8
-
Scala's implicits are its most distinguished feature. They are _the_ fundamental way to abstract over context. They represent a unified paradigm with an extremely varied number of use cases, among them: implementing type classes, establishing context, dependency injection, expressing capabilities, computing new types and proving relationships between them.
8
+
Scala's implicits are its most distinguished feature. They are _the_ fundamental way to abstract over context. They represent a unified paradigm with a great variety of use cases, among them: implementing type classes, establishing context, dependency injection, expressing capabilities, computing new types and proving relationships between them.
9
9
10
-
Following Haskell, Scala was probably the second popular language to have some form of implicits. Other languages have followed suit. E.g Rust's traits or Swift's protocol extensions. Design proposals are also on the table for Kotlin as [compile time dependency resolution](https://github.com/Kotlin/KEEP/blob/e863b25f8b3f2e9b9aaac361c6ee52be31453ee0/proposals/compile-time-dependency-resolution.md), for C# as [Shapes and Extensions](https://github.com/dotnet/csharplang/issues/164)
10
+
Following Haskell, Scala was the second popular language to have some form of implicits. Other languages have followed suit. E.g Rust's traits or Swift's protocol extensions. Design proposals are also on the table for Kotlin as [compile time dependency resolution](https://github.com/Kotlin/KEEP/blob/e863b25f8b3f2e9b9aaac361c6ee52be31453ee0/proposals/compile-time-dependency-resolution.md), for C# as [Shapes and Extensions](https://github.com/dotnet/csharplang/issues/164)
11
11
or for F# as [Traits](https://github.com/MattWindsor91/visualfsharp/blob/hackathon-vs/examples/fsconcepts.md). Implicits are also a common feature of theorem provers such as Coq or Agda.
12
12
13
-
Even though all these designs use widely different terminology, they are all variants of the core idea of _term inference_: given a type, the compiler synthesizes a "canonical" term that has that type. Scala embodies the idea in a purer form than most other languages: An implicit parameter directly leads to an inferred argument term that could also be written down explicitly. By contrast, typeclass based designs are less direct since they hide term inference behind some form of type classification and do not offer the option of writing the inferred quantities explicitly.
13
+
Even though these designs use widely different terminology, they are all variants of the core idea of _term inference_. Given a type, the compiler synthesizes a "canonical" term that has that type. Scala embodies the idea in a purer form than most other languages: An implicit parameter directly leads to an inferred argument term that could also be written down explicitly. By contrast, typeclass based designs are less direct since they hide term inference behind some form of type classification and do not offer the option of writing the inferred quantities (typically, dictionaries) explicitly.
14
14
15
15
Given that term inference is where the industry is heading, and given that Scala has it in a very pure form, how come implicits are not more popular? In fact, it's fair to say that implicits are at the same time Scala's most distinguished and most controversial feature. I believe this is due to a number of aspects that together make implicits harder to learn than necessary and also make it harder to prevent abuses.
16
16
@@ -39,22 +39,23 @@ Particular criticisms are:
39
39
40
40
None of the shortcomings is fatal, after all implicits are very widely used, and many libraries and applications rely on them. But together, they make code using implicits a lot more cumbersome and less clear than it could be.
41
41
42
+
Historically, many of these shortcomings come from the way implicits were gradually "discovered" in Scala. Scala originally had only implicit conversions with the intended use case of "extending" a classortraitafter it was defined, i.e. what is expressed by implicit classes in later versions of Scala. Implicit parameters and instance definitions came later in 2006 and picked similar syntax since it seemed convenient. For the same reason, no effort was made to distinguish implicit imports or arguments from normal ones.
43
+
42
44
ExistingScala programmers by and large have gotten used to the status quo and see little need for change. Butfor newcomers this status quo presents a big hurdle. I believe if we want to overcome that hurdle, we should take a step back and allow ourselves to consider a radically new design.
43
45
44
46
###TheNewDesign
45
47
46
-
The following pages introduce a redesign of contextual abstractions in Scala. They introduce the followingnewconstructs:
48
+
The following pages introduce a redesign of contextual abstractions in Scala. They introduce four fundamentalnewconstructs:
47
49
48
-
1. A way to replace implicit _definitions_ by [implied instance definitions](./instance-defs.html). The core principle of the proposal is that, rather than
49
-
mixing the `implicit` modifier with a large number of features, we have a single way to define terms that can be synthesized for types.
50
+
1. [ImpliedInstances](./instance-defs.html) are a new way to define inferable terms. They replace implicit definitions. The core principle of the proposal is that, rather than mixing the `implicit` modifier with a large number of features, we have a single way to define terms that can be synthesized for types.
50
51
51
-
2. A [new syntax](./inferable-params.html) forimplicit _parameters_ and their _arguments_. Both are introduced with the same keyword, `given`. This unambiguously aligns parameters and arguments, solving a number of language warts. It also allows us to have several implicit parameter sections, and to have implicit parameters followed by normal ones.
52
+
2. [InferableParameters](./inferable-params.html) are a new syntaxforimplicit _parameters_ and their _arguments_. Both are introduced with the same keyword, `given`. This unambiguously aligns parameters and arguments, solving a number of language warts. It also allows us to have several implicit parameter sections, and to have implicit parameters followed by normal ones.
52
53
53
-
3. Anew form of import, [importimplied](./import-implied.html) thatspecificallyimportsimplicitdefinitionsandnothingelse. New-styleimplicitdefinitions_mustbe_importedwith`import implied`, aplainimportwillnolongerbringthemintoscope.
54
+
3. [ImpliedImports](./import-implied.html) are new form of importthatspecificallyimportsimplicitdefinitionsandnothingelse. New-styleimplicitdefinitions_mustbe_importedwith`import implied`, aplainimportwillnolongerbringthemintoscope.
54
55
55
-
4. Anew way to write [implicit conversions](./conversions.html) asimplied instances of a `Conversion` class. All other forms of implicit conversions will be phased out.
56
+
4. [ImplicitConversions](./conversions.html) are now expressed asimplied instances of a standard `Conversion` class. All other forms of implicit conversions will be phased out.
56
57
57
-
This section also contains pages describing other language features that are related to context abstraction. Theseare:
58
+
This section also contains pages describing other language features that are related to context abstraction. Theseare:
58
59
59
60
- [ContextBounds](./context-bounds.html), which carry over unchanged.
60
61
- [ExtensionMethods](./extension-methods.html) replace implicit classes in a way that integrates better with typeclasses.
@@ -72,9 +73,9 @@ This design thus avoids feature interactions and makes the language more consist
72
73
73
74
Could we achieve the same goals by tweaking existing implicits?After having tried for a long time, I believe now that this is impossible.
74
75
75
-
-First, some of the problems are clearly syntactic and require different syntax to solve.
76
-
-Second, there is the problem how to migrate. We cannot change the rules in mid-flight. At some stage of language evolution we need to accommodate both the new and the old rules. With a syntax change, this is easy: Introduce the new syntax withnew rules, support the old syntax for a while to facilitate cross compilation, deprecate and phase out the old syntax some times later. Keeping the same syntax does not offer this path, and in fact does not seem to offer any viable path for evolution
76
+
-First, some of the problems are clearly syntactic and require different syntax to solve them.
77
+
-Second, there is the problem how to migrate. We cannot change the rules in mid-flight. At some stage of language evolution we need to accommodate both the new and the old rules. With a syntax change, this is easy: Introduce the new syntax withnew rules, support the old syntax for a while to facilitate cross compilation, deprecate and phase out the old syntax at some later time. Keeping the same syntax does not offer this path, and in fact does not seem to offer any viable path for evolution
77
78
-Third, even if we would somehow succeed with migration, we still have the problem
78
79
how to teach this. We cannot make existing tutorials go away. Almost all existing tutorials start withimplicit conversions, which will go away; they use normal imports, which will go away, and they explain calls to methods withimplicit parameters by expanding them to plain applications, which will also go away. This means that we'd have
79
-
to add modifications and qualifications to all existing literature and courseware,likely causing more confusion with beginners instead of less. By contrast, with a new syntax there is a clear criterion: Any courseware that mentions `implicit` is outdated and should be replaced.
80
+
to add modifications and qualifications to all existing literature and courseware,likely causing more confusion with beginners instead of less. By contrast, with a new syntax there is a clear criterion: Any courseware that mentions `implicit` is outdated and should be replaced.
0 commit comments