1+ """
2+ Either{L, R} = Union{Const{L}, Identity{R}}
3+ Either{Int, Bool}(true) == Identity(true)
4+ Either{Int, Bool}(1) == Const(1)
5+ Either{String}("left") == Const("left")
6+ Either{String}(:anythingelse) == Identity(:anythingelse)
7+
8+ A very common type to represent Result or Failure.
9+ We reuse [`Identity`](@ref) as representing "Success" and [`Const`](@ref) for "Failure".
10+ """
111const Either{L, R} = Union{Const{L}, Identity{R}}
212Either {L, R} (x:: L ) where {L, R} = Const (x)
313Either {L, R} (x:: R ) where {L, R} = Identity (x)
@@ -12,6 +22,18 @@ Base.eltype(::Type{Either}) = Any
1222# Helpers for Either
1323# ------------------
1424
25+ """
26+ either(:left, bool_condition, "right")
27+
28+ If `bool_condition` is true, it returns the right value, wrapped into [Identity](@ref).
29+ Else returns left side, wrapped into [Const](@ref).
30+
31+ Example
32+ -------
33+ ```jldoctest
34+ either(:left, 1 < 2, "right") == Identity("right")
35+ ```
36+ """
1537function either (left_false, comparison:: Bool , right_true)
1638 comparison ? Identity (right_true) : Const (left_false)
1739end
@@ -36,27 +58,91 @@ macro either(expr::Expr)
3658 end )
3759end
3860
61+
62+ """
63+ flip_left_right(Const(1)) == Identity(1)
64+ flip_left_right(Identity(:whatever)) == Const(:whatever)
65+
66+ exchanges left and right, i.e. what was Const, becomes an Identity and the other way around.
67+ """
3968flip_left_right (x:: Const ) = Identity (x. value)
4069flip_left_right (x:: Identity ) = Const (x. value)
4170
71+ """
72+ iseither(::Const) = true
73+ iseither(::Identity) = true
74+ iseither(other) = false
75+
76+ check whether something is an [`Either`](@ref)
77+ """
4278iseither (:: Const ) = true
4379iseither (:: Identity ) = true
4480iseither (other) = false
4581
82+ """
83+ isleft(::Const) = true
84+ isleft(::Identity) = false
85+
86+ Identical to [`isconst`](@ref), but might be easier to read when working with `Either`.
87+ """
4688isleft (:: Const ) = true
4789isleft (:: Identity ) = false
4890
91+
92+ """
93+ isright(::Const) = false
94+ isright(::Identity) = true
95+
96+ Identical to [`isidentity`](@ref), but might be easier to read when working with `Either`.
97+ """
4998isright (:: Const ) = false
5099isright (:: Identity ) = true
51100
101+ """
102+ getleft(Const(:something)) == :something
103+ getleft(Identity(23)) # throws MethodError
104+
105+ Extract a value from a "left" `Const` value. Will result in loud error when used on anything else.
106+ """
52107getleft (e:: Const ) = e. value
53- # getleft(e::Identity) = nothing # throw an error if getleft is called on Identity
54108
55- # getright(e::Const) = nothing # throw an error if getright is called on Const
109+ """
110+ getright(Identity(23)) == 23
111+ getright(Const(:something)) # throws MethodError
112+
113+ Extract a value from a "right" `Identity` value. Will result in loud error when used on anything else.
114+ Identical to `Base.get` but explicit about the site (and not defined for other things)
115+ """
56116getright (e:: Identity ) = e. value
57117
118+
119+ """
120+ getleftOption(Identity(23)) == Option()
121+ getleftOption(Const(:something)) == Option(:something)
122+
123+ Convert to option, assuming you want to have the left value be preserved.
124+ """
58125getleftOption (e:: Const ) = Option (e. value)
59126getleftOption (e:: Identity ) = Option ()
60127
128+ """
129+ getrightOption(Identity(23)) == Option(23)
130+ getrightOption(Const(:something)) == Option()
131+
132+ Convert to option, assuming you want to have the right value be preserved.
133+ Identical to `getOption`, just explicit about the site.
134+ """
61135getrightOption (e:: Const ) = Option ()
62136getrightOption (e:: Identity ) = e
137+
138+
139+
140+ """
141+ getOption(Identity(23)) == Option(23)
142+ getOption(Const(:something)) == Option()
143+
144+ Convert to option, assuming you want to have the right value be preserved, and the left value represented as
145+ `Option()`.
146+ """
147+ getOption (e:: Const ) = Option ()
148+ getOption (e:: Identity ) = e
0 commit comments