Skip to content

Commit 3832a22

Browse files
committed
Add Some, coalesce, notnothing
1 parent 8e106e8 commit 3832a22

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
@@ -217,6 +217,8 @@ Currently, the `@compat` macro supports the following syntaxes:
217217

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

220+
* `Some{T}` wraps `T` to signify that a result of `T<:Void` is expected ([#23642]).
221+
220222
## Renaming
221223

222224

@@ -391,6 +393,7 @@ includes this fix. Find the minimum version from there.
391393
[#23412]: https://github.com/JuliaLang/julia/issues/23412
392394
[#23427]: https://github.com/JuliaLang/julia/issues/23427
393395
[#23570]: https://github.com/JuliaLang/julia/issues/23570
396+
[#23642]: https://github.com/JuliaLang/julia/issues/23642
394397
[#23666]: https://github.com/JuliaLang/julia/issues/23666
395398
[#23757]: https://github.com/JuliaLang/julia/issues/23757
396399
[#23812]: https://github.com/JuliaLang/julia/issues/23812

src/Compat.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,48 @@ end
994994
export axes
995995
end
996996

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

9991041
end # module Compat

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,18 @@ end
10351035
@test axes(1) == ()
10361036
@test axes(1,1) == 1:1
10371037

1038+
# 0.7.0-DEV.3017
1039+
@test isa(Some(1), Some{Int})
1040+
@test convert(Some{Float64}, Some(1)) == Some(1.0)
1041+
@test convert(Void, nothing) == nothing
1042+
@test_throws MethodError convert(Void, 1)
1043+
@test Some(nothing) != nothing
1044+
@test coalesce(Some(1)) == 1
1045+
@test coalesce(nothing) == nothing
1046+
@test coalesce(nothing, Some(1), Some(2)) == 1
1047+
@test Compat.notnothing(1) == 1
1048+
@test_throws ArgumentError Compat.notnothing(nothing)
1049+
10381050
if VERSION < v"0.6.0"
10391051
include("deprecated.jl")
10401052
end

0 commit comments

Comments
 (0)