Skip to content

Commit

Permalink
Add Some, coalesce, notnothing
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Dec 20, 2017
1 parent 22bd8dd commit 0e64ae1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e64ae1

Please sign in to comment.