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

Julia 0.6 TODO: write awesome promotion rules #273

Closed
timholy opened this issue Feb 4, 2017 · 5 comments
Closed

Julia 0.6 TODO: write awesome promotion rules #273

timholy opened this issue Feb 4, 2017 · 5 comments
Milestone

Comments

@timholy
Copy link
Member

timholy commented Feb 4, 2017

Between ColorTypes and #272, we have some useful but imperfect promotion rules that I propose to fix once we can rely on the awesome new type system. Here's one simple example of current failings:

julia> using ColorTypes

julia> promote_type(RGB1{Float32}, RGB1{Float32})
ColorTypes.RGB1{Float32}

julia> promote_type(RGB1{Float32}, RGB1{Float64})
ColorTypes.RGB{Float64}

This behavior is a consequence of these rules. The ideal way to write this would be to separately promote the "inner" type (Float32 or Float64) and the "outer" type (e.g., RGB1{<:Fractional}). I suffered through doing something similar with conversion (e.g., convert(RGB, c) without specifying the "inner" type) on julia 0.4, and it wasn't fun: getting the dispatch working properly for types with only a typevar inner type was basically black art and strongly dependent on the order of function definitions. I basically don't have the time/patience to do this again. But once we can rely on the new type system, this should be much more pleasant.

@kimikage
Copy link
Collaborator

Can they move to ColorTypes.jl? (0646395)

Colors.jl/src/Colors.jl

Lines 9 to 11 in 0ceb396

# TODO: why these types are defined here? Can they move to ColorTypes.jl?
AbstractAGray{C<:AbstractGray,T} = AlphaColor{C,T,2}
AbstractGrayA{C<:AbstractGray,T} = ColorAlpha{C,T,2}

Base.promote_rule(::Type{C3}, ::Type{Cgray}) where {C3<:Color3,Cgray<:AbstractGray} = base_colorant_type(C3){promote_type(eltype(C3), eltype(Cgray))}
Base.promote_rule(::Type{C3}, ::Type{Cagray}) where {C3<:Color3,Cagray<:AbstractAGray} = alphacolor(base_colorant_type(C3)){promote_type(eltype(C3), eltype(Cagray))}
Base.promote_rule(::Type{C3}, ::Type{Cgraya}) where {C3<:Color3,Cgraya<:AbstractGrayA} = coloralpha(base_colorant_type(C3)){promote_type(eltype(C3), eltype(Cgraya))}
Base.promote_rule(::Type{C3}, ::Type{Cgray}) where {C3<:Transparent3,Cgray<:AbstractGray} = base_colorant_type(C3){promote_type(eltype(C3), eltype(Cgray))}
Base.promote_rule(::Type{C3}, ::Type{Cgray}) where {C3<:Transparent3,Cgray<:TransparentGray} = base_colorant_type(C3){promote_type(eltype(C3), eltype(Cgray))}

@timholy
Copy link
Member Author

timholy commented Nov 27, 2019

Those seem like reasonable candidates for moving.

@kimikage
Copy link
Collaborator

@kimikage
Copy link
Collaborator

I wrote awful promotion rules in ColorTypes.jl on Julia 1.3 (cf. JuliaGraphics/ColorTypes.jl#170). 😄
The rules will take effect in the next release.

@timholy
Copy link
Member Author

timholy commented Feb 23, 2020

They're beautiful, not awful. Though I know exactly what you mean. So glad to have this fixed!

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

2 participants