Skip to content

Commit

Permalink
Add Nothing and Cvoid
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Dec 21, 2017
1 parent 0e64ae1 commit 2655456
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `indices` is now `axes` ([#25057]).

* `Void` is now `Nothing` with an alias `Cvoid` for C interop ([#25162]).

## New macros

* `@__DIR__` has been added ([#18380])
Expand Down Expand Up @@ -413,3 +415,4 @@ includes this fix. Find the minimum version from there.
[#25012]: https://github.com/JuliaLang/julia/issues/25012
[#25056]: https://github.com/JuliaLang/julia/issues/25056
[#25057]: https://github.com/JuliaLang/julia/issues/25057
[#25162]: https://github.com/JuliaLang/julia/issues/25162
31 changes: 19 additions & 12 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,13 @@ end
export axes
end

# 0.7.0-DEV.3137
@static if !isdefined(Base, :Nothing)
const Nothing = Void
const Cvoid = Void
export Nothing, Cvoid
end

@static if !isdefined(Base, :Some)
import Base: promote_rule, convert
if VERSION >= v"0.6.0"
Expand All @@ -968,35 +975,35 @@ end
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}
promote_rule(::Type{Some{T}}, ::Type{Nothing}) where {T} = Union{Some{T}, Nothing}
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)
convert(::Type{Union{Some{T}, Nothing}}, x::Some) where {T} = convert(Some{T}, x)
convert(::Type{Union{T, Nothing}}, 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}
promote_rule{T}(::Type{Some{T}}, ::Type{Nothing}) = Union{Some{T}, Nothing}
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)
convert{T}(::Type{Union{Some{T}, Nothing}}, x::Some) = convert(Some{T}, x)
convert{T}(::Type{Union{T, Nothing}}, x::Any) = convert(T, x)
""")
end
convert(::Type{Void}, x::Any) = throw(MethodError(convert, (Void, x)))
convert(::Type{Void}, x::Void) = nothing
convert(::Type{Nothing}, x::Any) = throw(MethodError(convert, (Nothing, x)))
convert(::Type{Nothing}, x::Nothing) = nothing
coalesce(x::Any) = x
coalesce(x::Some) = x.value
coalesce(x::Void) = nothing
coalesce(x::Nothing) = 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...)
coalesce(x::Nothing, y...) = coalesce(y...)
#coalesce(x::Union{Nothing, Missing}, y...) = coalesce(y...)
notnothing(x::Any) = x
notnothing(::Void) = throw(ArgumentError("nothing passed to notnothing"))
notnothing(::Nothing) = throw(ArgumentError("nothing passed to notnothing"))
export Some, coalesce
else
import Base: notnothing
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,10 @@ end
@test axes(1) == ()
@test axes(1,1) == 1:1

# 0.7.0-DEV.3137
@test Nothing === (isdefined(Base, :Nothing) ? Base.Nothing : Base.Void)
@test Nothing === Cvoid

# 0.7.0-DEV.3017
@test isa(Some(1), Some{Int})
@test convert(Some{Float64}, Some(1)) == Some(1.0)
Expand Down

0 comments on commit 2655456

Please sign in to comment.