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
This package defines julia implementations for the common types `Option` (aka `Maybe`), `Either` and `Try`, as well as one extra type `ContextManager` which mimics Python's `with`-ContextManager.
4
+
This package defines julia implementations for the common types `Option` (aka `Maybe`), `Either` and `Try`, as well as
5
+
one extra type `ContextManager` which mimics Python's `with`-ContextManager.
5
6
7
+
Unlike typical implementations of `Option`, and `Either` which define them as new separate type-hierarchies, in
8
+
`DataTypesBasic` both actually share a common part: `Identity`.
6
9
7
-
## Option
8
10
9
-
`Option{T} = Union{Identity{T}, Nothing}`
11
+
Identity
12
+
--------
13
+
14
+
`Identity` is the most basic container you can imagine. It just contains one value and always one value. It is similar
15
+
to `Base.Some`, however with one notable difference. While `Some([]) != Some([])` (because it is treated more
16
+
like `Ref`), for `Identity` we have `Identity([]) == Identity([])`, as `Identity` works like a Container.
17
+
18
+
```jldoctest
19
+
julia> a = Identity(3)
20
+
Identity(3)
21
+
julia> map(x -> 2x, a)
22
+
Identity(6)
23
+
julia> foreach(println, a)
24
+
3
25
+
julia> for i in Identity("hello")
26
+
print("$i world")
27
+
end
28
+
hello world
29
+
```
30
+
31
+
Think of `Identity` as lifting a value into the world of containers. `DataTypesBasic` knows a lot about how to convert
32
+
an `Identity` container to a `Vector` or else. This will come in handy soon. For now it is important to understand
33
+
that wrapping some value `42` into `Identity(42)` makes it interactable on a container-level.
34
+
35
+
Nothing
36
+
-------
37
+
38
+
`DataTypesBasic` makes heavy use of the builtin `Base.Nothing`. In the context here, `nothing` is interpreted as an
39
+
empty container, something without any element. Or from another perspective: `nothing` works like a short-cycling abort.
40
+
41
+
42
+
Const
43
+
-----
44
+
45
+
`Const` looks like `Identity`, but behaves like `Nothing`. Its name suggests that whatever is in it will stay
46
+
**const**ant. It behaves like an empty container, and can also be seen as aborting a program. Compared to `Nothing`,
47
+
the `Const` container not only aborts, but further returns some useful abort-info.
48
+
49
+
```julia
50
+
struct Identity{T}
51
+
value::T
52
+
end
53
+
struct Const{T}
54
+
value::T
55
+
end
56
+
```
57
+
58
+
```jldoctest
59
+
julia> a = Const(3)
60
+
Const(3)
61
+
julia> map(x -> 2x, a)
62
+
Const(3)
63
+
julia> foreach(println, a)
64
+
65
+
julia> for i in Identity("hello")
66
+
print("$i world")
67
+
end
68
+
69
+
```
70
+
71
+
*******
72
+
73
+
Option
74
+
-------
75
+
76
+
Option is a container which has either 1 value or 0. Having `Identity` and `Nothing` already at hand, it is defined as
0 commit comments