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
Unused parameter are values that are know not to be used. Parameters of methods and functions
14
-
can be declared as unused. Those parameters wont be usable for computations, thought they can
15
-
be used as arguments to other unused parameters.
35
+
These constraint that only depend on the types at the call site are completly resolved at compile time and never used at runtime.
36
+
As these parameters are never used at runtime there is not real need to have them around, but they still need to be
37
+
present at runtime to be able to do separate compilation and retain binary compatiblity. Unused parameters are contractually
38
+
obligated to not be used at runtime, enforcing the essence of evidences on types and allows them to always be optimized away.
39
+
40
+
41
+
How to define unused parameter?
42
+
-------------------------------
43
+
Parameters of methods and functions can be declared as unused, placing `unused` at the start of the parameter list (like `implicit`).
16
44
17
45
```scala
18
46
defmethodWithUnusedEv(unused ev: Ev):Int=42
@@ -21,8 +49,17 @@ val lambdaWithUnusedEv: unused Ev => Int =
21
49
unused (ev: Ev) =>42
22
50
```
23
51
24
-
Not only parameters can be marked as unused, `val` and `def` can also be marked with `unused`.
25
-
The will also only be usable as arguments to `unused` parameters.
52
+
Those parameters will not be usable for computations, thought they can be used as arguments to other `unused` parameters.
53
+
54
+
```scala
55
+
defmethodWithUnusedInt1(unused i: Int):Int=
56
+
i +42// ERROR: can not use i
57
+
58
+
defmethodWithUnusedInt2(unused i: Int):Int=
59
+
methodWithUnusedInt1(i) // OK
60
+
```
61
+
62
+
Not only parameters can be marked as unused, `val` and `def` can also be marked with `unused`. These will also only be usable as arguments to `unused` parameters.
26
63
27
64
```scala
28
65
unused valunusedEvidence:Ev= ...
@@ -49,9 +86,9 @@ methodWithUnusedEv(evidence1)
49
86
methodWithUnusedEv(unusedEvidence2)
50
87
```
51
88
52
-
State machine example
53
-
---------------------
54
-
The following examples shows an implementation of a simple state machine which can be in a state `On` or `Off`.
89
+
State machine with unused evidence example
90
+
------------------------------------------
91
+
The following examples is an extended implementation of a simple state machine which can be in a state `On` or `Off`.
55
92
The machine can change state from `Off` to `On` with `turnedOn` only if it is currently `Off`,
56
93
conversely from `On` to `Off` with `turnedOff` only if it is currently `On`. These last constraint are
57
94
captured with the `IsOff[S]` and `IsOn[S]` implicit evidence only exist for `IsOff[Off]` and `InOn[On]`.
@@ -74,16 +111,19 @@ final class Off extends State
0 commit comments