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

Fix type-instability of ∘ and Some when wrapping types #35980

Merged
merged 4 commits into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -873,10 +873,28 @@ julia> ∘(fs...)(3)
```
"""
function ∘ end

struct ComposedFunction{F,G} <: Function
f::F
g::G
ComposedFunction{F, G}(f, g) where {F, G} = new{F, G}(f, g)
ComposedFunction(f, g) = new{Core.Typeof(f),Core.Typeof(g)}(f, g)
end

(c::ComposedFunction)(x...) = c.f(c.g(x...))

∘(f) = f
∘(f, g) = (x...)->f(g(x...))
∘(f, g) = ComposedFunction(f, g)
∘(f, g, h...) = ∘(f ∘ g, h...)

function show(io::IO, c::ComposedFunction)
show(io, c.f)
print(io, " ∘ ")
show(io, c.g)
end

show(io::IO, ::MIME"text/plain", c::ComposedFunction) = show(io, c)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a fallback for this.

Copy link
Member Author

@tkf tkf May 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, text/plain output is rather ugly:

julia> uppercase∘first
(::Base.ComposedFunction{typeof(uppercase),typeof(first)}) (generic function with 1 method)

I think this is due to show(io::IO, ::MIME"text/plain", f::Function).

But defining show here was incorrect because MIME is not defined here. I moved it to show.jl where show(io::IO, ::MIME"text/plain", f::Function) is defined.

With this definition, I get

julia> uppercase∘first
uppercase ∘ first

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, I was missing that it's a subtype of Function.


"""
!f::Function

Expand Down
2 changes: 2 additions & 0 deletions base/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct Some{T}
value::T
end

Some(::Type{T}) where {T} = Some{Type{T}}(T)

promote_rule(::Type{Some{T}}, ::Type{Some{S}}) where {T, S<:T} = Some{T}

nonnothingtype(::Type{T}) where {T} = Core.Compiler.typesubtract(T, Nothing)
Expand Down
2 changes: 2 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Base.promote_rule(::Type{T19714}, ::Type{Int}) = T19714
@test ∘(FreeMagma(1), FreeMagma(2)) === FreeMagma((1,2))
@test ∘(FreeMagma(1), FreeMagma(2), FreeMagma(3)) === FreeMagma(((1,2), 3))
@test ∘(FreeMagma(1), FreeMagma(2), FreeMagma(3), FreeMagma(4)) === FreeMagma((((1,2), 3), 4))

@test fieldtypes(typeof(Float64 ∘ Int)) == (Type{Float64}, Type{Int64})
end

@testset "function negation" begin
Expand Down
3 changes: 3 additions & 0 deletions test/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ using Base: notnothing
# isnothing()
@test !isnothing(1)
@test isnothing(nothing)

# type stability
@test fieldtype(typeof(Some(Int)), 1) === Type{Int}