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
It is now mostly confusing and does not convey a new point. There was a point once to it,
in an earlier design where the two roles of an implicit parameters were separated. Then
the example was much nicer. It was left in, to show that it's in principle still possible
to do this even if the two parameter roles are not separated. But without that discussion
it misses context.
Copy file name to clipboardExpand all lines: docs/docs/reference/witnesses/witness-params.md
-25
Original file line number
Diff line number
Diff line change
@@ -46,31 +46,6 @@ def g with (xy: (A, B))
46
46
g with ((a, b))
47
47
```
48
48
49
-
## Application: Dependency Injection
50
-
51
-
Witnesses can be used for dependency injection with constructor parameters. As an example, say we have four components `C1,...,C4` each of which depend on some subset of the other components. Constructor-based dependency injection defines these components as classes with explicitly passed parameters. E.g.,
52
-
```scala
53
-
classC1(c2: C2, c3: C3) { ... }
54
-
classC2(c1: C1, c4: C4) { ... }
55
-
classC3(c2: C3, c4: C4) { ... }
56
-
classC4(c1: C1, c3: C3, c3: C3) { ... }
57
-
```
58
-
The components can then be "wired together" by defining a set of local witnesses:
Note that component dependencies in `C1, ..., C4` are _not_ defined themselves as implicit parameters. This prevents components from spreading into the implicit namespace of other components and keeps the wiring strictly to the interface of these modules.
68
-
69
-
This scheme is essentially what MacWire does. MacWire was implemented as a macro library. It requires whitebox macros which will no longer be supported in Scala 3.
70
-
71
-
I considered for a while an alternative design where the two notions of an implicit parameter (argument gets synthesized vs. parameter is itself available as an implicit value) are separated. This would allow a nicer expression of component assembly which would not require that dependencies are repeated in the witnesses. The most significant downside of the alternative design is that it's likely to induce choice fatigue. In most cases, implicit parameters should be available itself as a witness, so asking for an opt-in each time a parameter is defined
72
-
became quickly tiresome.
73
-
74
49
## Implicit Function Types and Closures
75
50
76
51
Implicit function types are expressed using the new reserved operator `|=>`. Examples:
0 commit comments