@@ -16,7 +16,7 @@ When saying that they have no effect on the runtime we do not only mean side eff
16
16
like IO, field mutation, exceptions and so on. We also imply that if a function receives
17
17
a phantom its result will not be affected by this argument.
18
18
19
- As phantom do not live at runtime they cannot be subtypes of ` scala.Any ` , which defines
19
+ As phantoms do not live at runtime they cannot be subtypes of ` scala.Any ` , which defines
20
20
methods such as ` hashCode ` , ` equals ` , ` getClass ` , ` asInstanceOf ` and ` isInstanceOf ` .
21
21
All these operations cannot exist on phantoms as there will not be an underlying object
22
22
instance at runtime. At first glance this could look like a limitation, but in fact not
@@ -49,13 +49,13 @@ In fact we allow multiple phantom universes to exist.
49
49
+---------+ +-------------------+ +------------------------+
50
50
```
51
51
52
- Inside a universe it types support the full Dotty type system. But we cannot mix types from
53
- different universes with ` & ` , ` | ` or in type bounds. Each type must be fully defined one universe.
52
+ Inside a universe the full Dotty type system is supported . But we cannot mix types from
53
+ different universes with ` & ` , ` | ` or in type bounds. Each type must be fully defined in a single universe.
54
54
55
55
56
56
Implement your own phantom type
57
57
-------------------------------
58
- Phantom types are definded by an ` object ` extending ` scala.Phantom ` . This object will represent
58
+ Phantom types are defined by an ` object ` extending ` scala.Phantom ` . This object will represent
59
59
a universe of phantom types that is completely separated from types in ` scala.Any ` or other
60
60
phantom universes. We can define our phantom universe ` MyPhantoms ` .
61
61
@@ -78,7 +78,7 @@ of the phantom types in `MyPhantoms`, these bounds are `protected` and can not b
78
78
from outside ` MyPhantoms ` unless an alias is defined for them.
79
79
80
80
New phantom types can be defined using ` type XYZ <: OtherPhantom ` (where ` >: MyPhantom.Nothing `
81
- will be inferred), this would be the equivalent of ` class XYZ extends OtherClass ` on a types
81
+ will be inferred), this would be the equivalent of ` class XYZ extends OtherClass ` on types
82
82
only (no runtime definitions). Or aliased with ` type MyAny = OtherPhantom ` . Within ` MyPhantoms `
83
83
it is possible to refer to ` MyPhantoms.Any ` and ` MyPhantoms.Nothing ` with ` this.Any ` and
84
84
` this.Nothing ` (or just ` Any ` and ` Nothing ` but not recommended). Using this we will define
@@ -94,7 +94,7 @@ object MyPhantoms extends Phantom {
94
94
```
95
95
96
96
Values of phantom type can be created using the ` protected def assume ` . This value can be
97
- used as a value of this phantom type as it's type is ` this.Nothing ` (or ` MyPhantoms.Nothing ` ).
97
+ used as a value of this phantom type as its type is ` this.Nothing ` (or ` MyPhantoms.Nothing ` ).
98
98
Usually this value will be used to define a ` implicit def ` that returns the phantom with a more
99
99
precise type. In our example we will only create values of type ` Pinky ` and ` Clyde `
100
100
@@ -136,8 +136,8 @@ What happens with Phantoms at runtime?
136
136
137
137
Disclaimer: Most of phantom erasure is implemented, but not all of is has been merged in ` dotty/master ` yet.
138
138
139
- As phantom have no effect on the result of a method invocation we just remove them for the call an definition.
140
- The evaluation of the phantom parameter is still be done unless it can be optimized away.
139
+ As phantoms have no effect on the result of a method invocation we just remove them for the call and definition.
140
+ The evaluation of the phantom parameter is still done unless it can be optimized away.
141
141
By removing them we also restrict overloading as ` def f() ` and ` def f(x: MyPhantom) ` will
142
142
have the same signature in the bytecode, just use different names to avoid this.
143
143
0 commit comments