|
960 | 960 | export axes |
961 | 961 | end |
962 | 962 |
|
| 963 | +@static if !isdefined(Base, :Some) |
| 964 | + import Base: promote_rule, convert |
| 965 | + if VERSION >= v"0.6.0" |
| 966 | + include_string(@__MODULE__, """ |
| 967 | + struct Some{T} |
| 968 | + value::T |
| 969 | + end |
| 970 | + promote_rule(::Type{Some{S}}, ::Type{Some{T}}) where {S,T} = Some{promote_type(S, T)} |
| 971 | + promote_rule(::Type{Some{T}}, ::Type{Void}) where {T} = Union{Some{T}, Void} |
| 972 | + convert(::Type{Some{T}}, x::Some) where {T} = Some{T}(convert(T, x.value)) |
| 973 | + convert(::Type{Union{Some{T}, Void}}, x::Some) where {T} = convert(Some{T}, x) |
| 974 | + convert(::Type{Union{T, Void}}, x::Any) where {T} = convert(T, x) |
| 975 | + """) |
| 976 | + else |
| 977 | + include_string(@__MODULE__, """ |
| 978 | + immutable Some{T} |
| 979 | + value::T |
| 980 | + end |
| 981 | + promote_rule{S,T}(::Type{Some{S}}, ::Type{Some{T}}) = Some{promote_type(S, T)} |
| 982 | + promote_rule{T}(::Type{Some{T}}, ::Type{Void}) = Union{Some{T}, Void} |
| 983 | + convert{T}(::Type{Some{T}}, x::Some) = Some{T}(convert(T, x.value)) |
| 984 | + convert{T}(::Type{Union{Some{T}, Void}}, x::Some) = convert(Some{T}, x) |
| 985 | + convert{T}(::Type{Union{T, Void}}, x::Any) = convert(T, x) |
| 986 | + """) |
| 987 | + end |
| 988 | + convert(::Type{Void}, x::Any) = throw(MethodError(convert, (Void, x))) |
| 989 | + convert(::Type{Void}, x::Void) = nothing |
| 990 | + coalesce(x::Any) = x |
| 991 | + coalesce(x::Some) = x.value |
| 992 | + coalesce(x::Void) = nothing |
| 993 | + #coalesce(x::Missing) = missing |
| 994 | + coalesce(x::Any, y...) = x |
| 995 | + coalesce(x::Some, y...) = x.value |
| 996 | + coalesce(x::Void, y...) = coalesce(y...) |
| 997 | + #coalesce(x::Union{Void, Missing}, y...) = coalesce(y...) |
| 998 | + notnothing(x::Any) = x |
| 999 | + notnothing(::Void) = throw(ArgumentError("nothing passed to notnothing")) |
| 1000 | + export Some, coalesce |
| 1001 | +else |
| 1002 | + import Base: notnothing |
| 1003 | +end |
| 1004 | + |
963 | 1005 | include("deprecated.jl") |
964 | 1006 |
|
965 | 1007 | end # module Compat |
0 commit comments