Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function MP.monomial_type(vars::Tuple{V,Vararg{Variable}}) where {V<:Variable}
return Monomial{sorted, length(sorted)}
end

_instantiate(::Type{Tuple{}}) = ()
_instantiate(::Type{T}) where {T<:Tuple} =
(Base.tuple_type_head(T)(), _instantiate(Base.tuple_type_tail(T))...)

function MP.monomial_type(::Type{T}) where {T<:Tuple{Variable,Vararg{Variable}}}
return monomial_type(tuple((V() for V in fieldtypes(T))...))
return monomial_type(_instantiate(T))
end
6 changes: 6 additions & 0 deletions test/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ end
@test @inferred(monomial_type(typeof((x,)))) == Monomial{(x,), 1}
@test @inferred(monomial_type(typeof((x, y)))) == Monomial{(x, y), 2}
@test @inferred(monomial_type(typeof((x, y, z)))) == Monomial{(x, y, z), 3}

# Test that `monomial_type` on Tuple types is constant-folded,
# so that branches on it are eliminated at compile time.
_mt_branch(::Type{V}) where {V} =
monomial_type(V) === Monomial{(x, y), 2} ? Int : String
@test @inferred(_mt_branch(typeof((x, y)))) === Int
end

@testset "conversion" begin
Expand Down
Loading