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? #19748

Closed
JeffBezanson opened this issue Dec 29, 2016 · 4 comments
Closed

promoting Nullable with anything? #19748

JeffBezanson opened this issue Dec 29, 2016 · 4 comments
Labels
missing data Base.missing and related functionality needs decision A decision on this change is needed

Comments

@JeffBezanson
Copy link
Member

JeffBezanson commented Dec 29, 2016

e79e846 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).

@JeffBezanson JeffBezanson added the needs decision A decision on this change is needed label Dec 29, 2016
@JeffBezanson JeffBezanson changed the title allow promoting Nullable with anything? promoting Nullable with anything? Dec 29, 2016
@tkelman tkelman added the missing data Base.missing and related functionality label Dec 29, 2016
@nalimilan
Copy link
Member

I see your point. Maybe just check that the promoted type is different from Any instead? In practice I'm not sure it's really different, but it could be simpler to explain/remember.

Though there's always a cost in making rules more complex. Do you have a use case in mind where the current simple rule would be problematic?

@JeffBezanson
Copy link
Member Author

There was a piece of code here: JuliaIO/JSON.jl#39 (comment)
that used vcat to combine values before assigning them to fields of another type. Since one was nullable, all values were converted to nullable and didn't match the expected field types.

@nalimilan
Copy link
Member

OK. A related issue is that conversion to nullable is allowed, but conversion from nullable is not. Not sure we have other cases like this where promotion gives a type which cannot be converted back to the original type, but that sounds potentially problematic (as in your example).

@KristofferC
Copy link
Member

Moved to JuliaAttic/Nullables.jl#6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
missing data Base.missing and related functionality needs decision A decision on this change is needed
Projects
None yet
Development

No branches or pull requests

4 participants