Comments (5)
The issue seems to be
julia> promote_type(NonlinearExpr, VariableRef, Float64)
NonlinearExpr (alias for GenericNonlinearExpr{GenericVariableRef{Float64}})
julia> promote_type(NonlinearExpr, Float64)
Any
julia> promote_type(VariableRef, NonlinearExpr, Float64)
Any
julia> promote_type(NonlinearExpr, Float64, VariableRef)
NonlinearExpr (alias for GenericNonlinearExpr{GenericVariableRef{Float64}})
julia> promote_type(Float64, NonlinearExpr, VariableRef)
Any
from jump.jl.
I guess it is because Float64
is transitively convertible to NonlinearExpr
via AffExpr
:
julia> promote_type(NonlinearExpr, Float64, VariableRef)
NonlinearExpr (alias for GenericNonlinearExpr{GenericVariableRef{Float64}})
julia> promote_type(NonlinearExpr, promote_type(Float64, VariableRef))
NonlinearExpr (alias for GenericNonlinearExpr{GenericVariableRef{Float64}})
from jump.jl.
Thinking more on this. Perhaps we do need to add a method that just makes NonlinearExpr(:+, Any[x])
from jump.jl.
Here's the reproducer:
julia> struct Foo; x::Int end
julia> struct Bar; x::Int end
julia> Base.convert(::Type{Foo}, b::Bar) = Foo(b.x)
julia> Base.convert(::Type{Bar}, x::Int) = Bar(x)
julia> Base.promote_rule(::Type{Foo}, ::Type{Bar}) = Foo
julia> Base.promote_rule(::Type{Bar}, ::Type{Int}) = Bar
julia> [Foo(1), Bar(2), 3]
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Foo
Closest candidates are:
convert(::Type{Foo}, ::Bar)
@ Main REPL[62]:1
convert(::Type{T}, ::T) where T
@ Base Base.jl:84
Foo(::Int64)
@ Main REPL[60]:1
...
Stacktrace:
[1] setindex!(A::Vector{Foo}, x::Int64, i1::Int64)
@ Base ./array.jl:1021
[2] (::Base.var"#114#115"{Vector{Foo}})(i::Int64, v::Int64)
@ Base ./array.jl:456
[3] afoldl(::Base.var"#114#115"{Vector{Foo}}, ::Int64, ::Foo, ::Bar, ::Int64)
@ Base ./operators.jl:546
[4] getindex(::Type{Foo}, ::Foo, ::Bar, ::Int64)
@ Base ./array.jl:455
[5] vect(::Foo, ::Vararg{Any})
@ Base ./array.jl:187
[6] top-level scope
@ REPL[66]:1
julia> [Foo(1), 3, Bar(2)]
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Foo
Closest candidates are:
convert(::Type{Foo}, ::Bar)
@ Main REPL[62]:1
convert(::Type{T}, ::T) where T
@ Base Base.jl:84
Foo(::Int64)
@ Main REPL[60]:1
...
Stacktrace:
[1] setindex!(A::Vector{Foo}, x::Int64, i1::Int64)
@ Base ./array.jl:1021
[2] (::Base.var"#114#115"{Vector{Foo}})(i::Int64, v::Int64)
@ Base ./array.jl:456
[3] afoldl(::Base.var"#114#115"{Vector{Foo}}, ::Int64, ::Foo, ::Int64, ::Bar)
@ Base ./operators.jl:545
[4] getindex(::Type{Foo}, ::Foo, ::Int64, ::Bar)
@ Base ./array.jl:455
[5] vect(::Foo, ::Vararg{Any})
@ Base ./array.jl:187
[6] top-level scope
@ REPL[67]:1
julia> [Bar(2), Foo(1), 3]
3-element Vector{Any}:
Bar(2)
Foo(1)
3
julia> [Bar(2), 3, Foo(1)]
3-element Vector{Any}:
Bar(2)
3
Foo(1)
julia> [3, Foo(1), Bar(2)]
3-element Vector{Any}:
3
Foo(1)
Bar(2)
julia> [3, Foo(1), Bar(2)]
3-element Vector{Any}:
3
Foo(1)
Bar(2)
julia> [3, Bar(2), Foo(1)]
3-element Vector{Any}:
3
Bar(2)
Foo(1)
from jump.jl.
Asked on discourse https://discourse.julialang.org/t/associativity-of-promote-type/109958
from jump.jl.
Related Issues (20)
- @variable macro fails to resolve variables in 1.18.0 HOT 2
- jump.dev is down HOT 1
- Model printing is rather verbose HOT 5
- Diagnostics request: using variable in variable container bounds HOT 4
- && and || do not short-circuit in macros HOT 3
- Add support for MOI.ScalarQuadraticCoefficientChange
- Tools to test JuMP models
- NumFOCUS: GSoC 2024 Update HOT 1
- Increase performance of SOC constraints HOT 4
- Iterating SparseAxisArray does not preserve order HOT 4
- How to improve the speed to build a complex model? HOT 2
- Coefficients of complex variables created with a GenericModel are always Float64
- LinearAlgebra.hermitian is incorrect
- *(::Real, ::Hermitian) is not hermitian HOT 5
- Cannot `convert` an object of type Float64 to an object of type JuMP.NonlinearExpr HOT 3
- Documentation Request: List whether a solver supports Indicator Constraints HOT 4
- Bullet point alignment in bibliography HOT 9
- Multiplication of matrix expression and variables leads to stack overflow and matmul error HOT 4
- Multiple Ranges for variables HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jump.jl.