From 0e64ae1ee6a757d5e336afd483b52318bfb43019 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Wed, 20 Dec 2017 13:24:46 -0800 Subject: [PATCH] Add Some, coalesce, notnothing --- README.md | 3 +++ src/Compat.jl | 42 ++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 12 ++++++++++++ 3 files changed, 57 insertions(+) diff --git a/README.md b/README.md index cae06b6ed..52d5c87b2 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `get` do-block syntax supported when using `ENV` ([#23412]). +* `Some{T}` wraps `T` to signify that a result of `T<:Void` is expected ([#23642]). + ## Renaming @@ -390,6 +392,7 @@ includes this fix. Find the minimum version from there. [#23412]: https://github.com/JuliaLang/julia/issues/23412 [#23427]: https://github.com/JuliaLang/julia/issues/23427 [#23570]: https://github.com/JuliaLang/julia/issues/23570 +[#23642]: https://github.com/JuliaLang/julia/issues/23642 [#23666]: https://github.com/JuliaLang/julia/issues/23666 [#23667]: https://github.com/JuliaLang/julia/issues/23667 [#23757]: https://github.com/JuliaLang/julia/issues/23757 diff --git a/src/Compat.jl b/src/Compat.jl index 32a9618ca..51f0084f7 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -960,6 +960,48 @@ end export axes end +@static if !isdefined(Base, :Some) + import Base: promote_rule, convert + if VERSION >= v"0.6.0" + include_string(@__MODULE__, """ + struct Some{T} + value::T + end + promote_rule(::Type{Some{S}}, ::Type{Some{T}}) where {S,T} = Some{promote_type(S, T)} + promote_rule(::Type{Some{T}}, ::Type{Void}) where {T} = Union{Some{T}, Void} + convert(::Type{Some{T}}, x::Some) where {T} = Some{T}(convert(T, x.value)) + convert(::Type{Union{Some{T}, Void}}, x::Some) where {T} = convert(Some{T}, x) + convert(::Type{Union{T, Void}}, x::Any) where {T} = convert(T, x) + """) + else + include_string(@__MODULE__, """ + immutable Some{T} + value::T + end + promote_rule{S,T}(::Type{Some{S}}, ::Type{Some{T}}) = Some{promote_type(S, T)} + promote_rule{T}(::Type{Some{T}}, ::Type{Void}) = Union{Some{T}, Void} + convert{T}(::Type{Some{T}}, x::Some) = Some{T}(convert(T, x.value)) + convert{T}(::Type{Union{Some{T}, Void}}, x::Some) = convert(Some{T}, x) + convert{T}(::Type{Union{T, Void}}, x::Any) = convert(T, x) + """) + end + convert(::Type{Void}, x::Any) = throw(MethodError(convert, (Void, x))) + convert(::Type{Void}, x::Void) = nothing + coalesce(x::Any) = x + coalesce(x::Some) = x.value + coalesce(x::Void) = nothing + #coalesce(x::Missing) = missing + coalesce(x::Any, y...) = x + coalesce(x::Some, y...) = x.value + coalesce(x::Void, y...) = coalesce(y...) + #coalesce(x::Union{Void, Missing}, y...) = coalesce(y...) + notnothing(x::Any) = x + notnothing(::Void) = throw(ArgumentError("nothing passed to notnothing")) + export Some, coalesce +else + import Base: notnothing +end + include("deprecated.jl") end # module Compat diff --git a/test/runtests.jl b/test/runtests.jl index af3593a81..86341f14d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1023,6 +1023,18 @@ end @test axes(1) == () @test axes(1,1) == 1:1 +# 0.7.0-DEV.3017 +@test isa(Some(1), Some{Int}) +@test convert(Some{Float64}, Some(1)) == Some(1.0) +@test convert(Void, nothing) == nothing +@test_throws MethodError convert(Void, 1) +@test Some(nothing) != nothing +@test coalesce(Some(1)) == 1 +@test coalesce(nothing) == nothing +@test coalesce(nothing, Some(1), Some(2)) == 1 +@test Compat.notnothing(1) == 1 +@test_throws ArgumentError Compat.notnothing(nothing) + if VERSION < v"0.6.0" include("deprecated.jl") end