Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promoting Nullable with anything? #6

Open
KristofferC opened this issue Jan 7, 2018 · 0 comments
Open

promoting Nullable with anything? #6

KristofferC opened this issue Jan 7, 2018 · 0 comments

Comments

@KristofferC
Copy link
Contributor

Moved from JuliaLang/julia#19748


e79e84654a40e8189e5dfbd55df232ae6c82a213 added the following definition:

promote_rule{S,T}(::Type{Nullable{S}}, ::Type{T}) = Nullable{promote_type(S, T)}

This might have unintended consequences. For example, we have:

julia> promote(1, "")
(1,"")

julia> promote(Nullable(1), "")
(Nullable{Any}(1),Nullable{Any}(""))

Given a Nullable and something completely unrelated, you get two Nullable{Any}s. I don't think two types should have a non-identity promotion just because one is Nullable. This was not obvious at the time.

I propose this definition instead:

function promote_rule{S,T}(::Type{Nullable{S}}, ::Type{T})
    R = promote_type(S, T)
    if R <: S || R <: T
    else
        if S <: R || T <: R
            return R
        end
    end
    return Nullable{R}
end

This says to leave the values alone if the promoted type (R) is a strict supertype of T or S, and otherwise promote as before. For example

julia> promote(Nullable(1),2.4)
(Nullable{Float64}(1.0),Nullable{Float64}(2.4))

julia> promote(Nullable(1),"")
(Nullable{Int64}(1),"")

This definition can still be type-inferred (since we can now handle constant conditions well).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant