What is wrong with this code? Can I fix it by implementing convert somehow?
const Maybe{T} = Union{Some{T},Nothing}
struct Wrapper{F}
f::F
end
struct WrappedPerson{T}
name::Wrapper{T}
age::Wrapper{T}
end
WrappedPerson{Maybe}(Wrapper(Some("Alice")), Wrapper(Some(25)))
# MethodError: Cannot `convert` an object of type
# Wrapper{Some{String}} to an object of type
# Wrapper{Union{Nothing, Some{T}} where T}
I think it’d be better fixed at the point where you’re making the wrappers. Rather than write
Wrapper(Some("Alice"))
you could write
Wrapper{Maybe}(Some("Alice"))
e.g.
julia> WrappedPerson{Maybe}(Wrapper{Maybe}(Some("Alice")), Wrapper{Maybe}(Some("Alice")))
WrappedPerson{Union{Nothing, Some{T}} where T}(Wrapper{Union{Nothing, Some{T}} where T}(Some("Alice")), Wrapper{Union{Nothing, Some{T}} where T}(Some("Alice")))
But yeah, I think you could also define some methods to Base.convert that turns a Wrapper{T} into Wrapper{Maybe} I guess. All of this seems pretty unidiomatic and unlikely to work well though. These are like sum types, but not well contained or bounded.