|
994 | 994 | export axes |
995 | 995 | end |
996 | 996 |
|
| 997 | +@static if !isdefined(Base, :Some) |
| 998 | + import Base: promote_rule, convert |
| 999 | + if VERSION >= v"0.6.0" |
| 1000 | + include_string(@__MODULE__, """ |
| 1001 | + struct Some{T} |
| 1002 | + value::T |
| 1003 | + end |
| 1004 | + promote_rule(::Type{Some{S}}, ::Type{Some{T}}) where {S,T} = Some{promote_type(S, T)} |
| 1005 | + promote_rule(::Type{Some{T}}, ::Type{Void}) where {T} = Union{Some{T}, Void} |
| 1006 | + convert(::Type{Some{T}}, x::Some) where {T} = Some{T}(convert(T, x.value)) |
| 1007 | + convert(::Type{Union{Some{T}, Void}}, x::Some) where {T} = convert(Some{T}, x) |
| 1008 | + convert(::Type{Union{T, Void}}, x::Any) where {T} = convert(T, x) |
| 1009 | + """) |
| 1010 | + else |
| 1011 | + include_string(@__MODULE__, """ |
| 1012 | + immutable Some{T} |
| 1013 | + value::T |
| 1014 | + end |
| 1015 | + promote_rule{S,T}(::Type{Some{S}}, ::Type{Some{T}}) = Some{promote_type(S, T)} |
| 1016 | + promote_rule{T}(::Type{Some{T}}, ::Type{Void}) = Union{Some{T}, Void} |
| 1017 | + convert{T}(::Type{Some{T}}, x::Some) = Some{T}(convert(T, x.value)) |
| 1018 | + convert{T}(::Type{Union{Some{T}, Void}}, x::Some) = convert(Some{T}, x) |
| 1019 | + convert{T}(::Type{Union{T, Void}}, x::Any) = convert(T, x) |
| 1020 | + """) |
| 1021 | + end |
| 1022 | + convert(::Type{Void}, x::Any) = throw(MethodError(convert, (Void, x))) |
| 1023 | + convert(::Type{Void}, x::Void) = nothing |
| 1024 | + coalesce(x::Any) = x |
| 1025 | + coalesce(x::Some) = x.value |
| 1026 | + coalesce(x::Void) = nothing |
| 1027 | + #coalesce(x::Missing) = missing |
| 1028 | + coalesce(x::Any, y...) = x |
| 1029 | + coalesce(x::Some, y...) = x.value |
| 1030 | + coalesce(x::Void, y...) = coalesce(y...) |
| 1031 | + #coalesce(x::Union{Void, Missing}, y...) = coalesce(y...) |
| 1032 | + notnothing(x::Any) = x |
| 1033 | + notnothing(::Void) = throw(ArgumentError("nothing passed to notnothing")) |
| 1034 | + export Some, coalesce |
| 1035 | +else |
| 1036 | + import Base: notnothing |
| 1037 | +end |
| 1038 | + |
997 | 1039 | include("deprecated.jl") |
998 | 1040 |
|
999 | 1041 | end # module Compat |
0 commit comments