GithubHelp home page GithubHelp logo

Comments (5)

odow avatar odow commented on June 11, 2024

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.

odow avatar odow commented on June 11, 2024

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.

odow avatar odow commented on June 11, 2024

Thinking more on this. Perhaps we do need to add a method that just makes NonlinearExpr(:+, Any[x])

from jump.jl.

odow avatar odow commented on June 11, 2024

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.

odow avatar odow commented on June 11, 2024

Asked on discourse https://discourse.julialang.org/t/associativity-of-promote-type/109958

from jump.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.