Skip to content

Commit 0e64ae1

Browse files
committed
Add Some, coalesce, notnothing
1 parent 22bd8dd commit 0e64ae1

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ Currently, the `@compat` macro supports the following syntaxes:
214214

215215
* `get` do-block syntax supported when using `ENV` ([#23412]).
216216

217+
* `Some{T}` wraps `T` to signify that a result of `T<:Void` is expected ([#23642]).
218+
217219
## Renaming
218220

219221

@@ -390,6 +392,7 @@ includes this fix. Find the minimum version from there.
390392
[#23412]: https://github.com/JuliaLang/julia/issues/23412
391393
[#23427]: https://github.com/JuliaLang/julia/issues/23427
392394
[#23570]: https://github.com/JuliaLang/julia/issues/23570
395+
[#23642]: https://github.com/JuliaLang/julia/issues/23642
393396
[#23666]: https://github.com/JuliaLang/julia/issues/23666
394397
[#23667]: https://github.com/JuliaLang/julia/issues/23667
395398
[#23757]: https://github.com/JuliaLang/julia/issues/23757

src/Compat.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,48 @@ end
960960
export axes
961961
end
962962

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+
9631005
include("deprecated.jl")
9641006

9651007
end # module Compat

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,18 @@ end
10231023
@test axes(1) == ()
10241024
@test axes(1,1) == 1:1
10251025

1026+
# 0.7.0-DEV.3017
1027+
@test isa(Some(1), Some{Int})
1028+
@test convert(Some{Float64}, Some(1)) == Some(1.0)
1029+
@test convert(Void, nothing) == nothing
1030+
@test_throws MethodError convert(Void, 1)
1031+
@test Some(nothing) != nothing
1032+
@test coalesce(Some(1)) == 1
1033+
@test coalesce(nothing) == nothing
1034+
@test coalesce(nothing, Some(1), Some(2)) == 1
1035+
@test Compat.notnothing(1) == 1
1036+
@test_throws ArgumentError Compat.notnothing(nothing)
1037+
10261038
if VERSION < v"0.6.0"
10271039
include("deprecated.jl")
10281040
end

0 commit comments

Comments
 (0)