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/phantom-types.md
+41-44Lines changed: 41 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -7,25 +7,21 @@ title: "Phantom Types"
7
7
What is a phantom type?
8
8
-----------------------
9
9
10
-
A phantom type is a manifestation of abstract type that has no effect on the runtime.
11
-
These are useful to prove static properties of the code using type evidences.
12
-
As they have no effect on the runtime they can be erased from the resulting code by
13
-
the compiler once it has shown the constraints hold.
14
-
15
-
When saying that a they have no effect on the runtime we do not only mean side effects
16
-
like IO, field mutation, exceptions and so on. We also imply that if a function receives
17
-
a phantom its result will not be affected by this argument.
18
-
19
-
As phantom do not live at runtime they cannot be subtypes of `scala.Any`, which deffines
20
-
methods such as `hashCode`, `equals`, `getClass`, `asInstanceOf` and `isInstanceOf`.
21
-
All these operations cannot exist on phantoms as there will not be an underlying object
22
-
instance at runtime. At first glace this could look like a limitation, but in fact not
23
-
having `asInstanceOf` will make constraints more reliable as it will not be possible to
24
-
downcast a phantom value to fake an evidence.
25
-
26
-
If they don't live in the universe bounded by `scala.Any` and `scala.Nothing` where do
27
-
they live? The answer is in their own type universes bounded by their phantom `Any` and `Nothing`.
10
+
A phantom type is a manifestation of an abstract type that has no effect on the runtime execution of the program.
11
+
Phantom types are useful to prove static properties of the code, using evidence or witnesses that the aforementioned properties hold at the type level.
12
+
The most significant property of phantom types is that they have no _effect_ on the runtime since they can be erased from the resulting code by the compiler once it has shown that the constraints hold.
13
+
14
+
When saying that they have no effect on the runtime we not only mean side effects
15
+
such as IO, field mutation, exceptions but also that the result of a function receiving a phantom will not be affected by this argument.
16
+
17
+
Phantoms are not represented by an object instance at runtime.
18
+
Subsequently, since phantoms do not live at runtime, they cannot be subtypes of `scala.Any`, which defines methods such as `hashCode`, `equals`, `getClass`, `asInstanceOf` and `isInstanceOf`.
19
+
At first glace this could look like a limitation.
20
+
However, by not having `asInstanceOf` makes constraints more reliable as it will not be possible to downcast a phantom value to fake an evidence.
21
+
22
+
An interesting question is the following: if they don't live in the _type universe_, bounded by `scala.Any` and `scala.Nothing`, where do they live? The answer is in their own type universes bounded by their phantom `Any` and `Nothing`.
28
23
In fact we allow multiple phantom universes to exist.
24
+
In the following figure we demonstrate three such universes.
29
25
30
26
```none
31
27
+-----+ +---------------+ +--------------------+
@@ -49,15 +45,16 @@ In fact we allow multiple phantom universes to exist.
0 commit comments