Skip to content

Commit 2a9b086

Browse files
supporting AbstractArray in general instead of Vector only
1 parent bc8efeb commit 2a9b086

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
## [2.0.0] - ?????
1010
### Added
1111
- `Base.get` and `Base.getindex` are now both implemented for `Const` (like they are implemented already for `Identity`), to simplify working with `Const`.
12+
- Vector conversions (`convert`) are now more generic and support AbstractArray in general
1213

1314
### Fixed
1415
- `Option(3)` works now

src/convert.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# conversions among concrete types
22
# ================================
33

4-
# Vector
5-
# ------
4+
# AbstractVector
5+
# --------------
6+
7+
# Because `AbstractVector <: AbstractArray`, and more especially `Base.typename(Vector) == Array`,
8+
# we are dispatching on AbstractArray instead of AbstractVector
69

7-
Base.convert(::Type{<:Vector}, x::Identity) = [x.value]
8-
Base.convert(::Type{<:Vector}, x::Nothing) = []
9-
Base.convert(::Type{<:Vector}, x::Const) = []
10+
Base.convert(T::Type{<:AbstractArray}, x::Identity) = convert(T, [x.value])
11+
Base.convert(T::Type{<:AbstractArray}, x::Const) = convert(T, [])
1012
# ContextManager is executed if someone asks for a Vector from ContextManager
11-
Base.convert(::Type{<:Vector}, x::ContextManager) = [x(identity)]
13+
Base.convert(T::Type{<:AbstractArray}, x::ContextManager) = convert(T, [x(identity)])
14+
1215

1316
# Const
1417
# -----
@@ -18,6 +21,7 @@ function Base.convert(::Type{Const}, x::Vector)
1821
Const(nothing) # == Option()
1922
end
2023

24+
2125
# Identity
2226
# --------
2327

@@ -48,7 +52,10 @@ Base.convert(::Type{Either{L, R}}, x::Identity{R}) where {L, R} = x # nothing t
4852
Base.convert(::Type{Either{L, R}}, x::Const) where {L, R} = Const(Base.convert(L, x.value))
4953
Base.convert(::Type{Either{L, R}}, x::Const{L}) where {L, R} = x # nothing to convert
5054

51-
Base.convert(::Type{Either{<:Any, R}}, x::Identity) where {R} = Identity(Base.convert(R, x.value))
5255
Base.convert(::Type{Either{<:Any, R}}, x::Identity{R}) where {R} = x # nothing to convert
53-
Base.convert(::Type{Either{L, <:Any}}, x::Const) where {L} = Const(Base.convert(L, x.value))
56+
Base.convert(::Type{Either{<:Any, R}}, x::Identity) where {R} = Identity(Base.convert(R, x.value))
5457
Base.convert(::Type{Either{L, <:Any}}, x::Const{L}) where {L} = x # nothing to convert
58+
Base.convert(::Type{Either{L, <:Any}}, x::Const) where {L} = Const(Base.convert(L, x.value))
59+
60+
Base.convert(::Type{Either}, x::Identity) = x
61+
Base.convert(::Type{Either}, x::Const) = x

0 commit comments

Comments
 (0)