Skip to content

Commit 3b60aa9

Browse files
committed
Provide Base.length(::AbstractJuMPScalar)
``` using JuMP, Ipopt, Distances model = Model(Ipopt.Optimizer) @variable(model, a) @variable(model, b) euclidean(a, b) ``` Results in: ``` ERROR: MethodError: no method matching length(::VariableRef) Closest candidates are: length(::Union{Base.KeySet, Base.ValueIterator}) @ Base abstractdict.jl:58 length(::Union{LinearAlgebra.Adjoint{T, S}, LinearAlgebra.Transpose{T, S}} where {T, S}) @ LinearAlgebra /builddirs/julia/usr/share/julia/stdlib/v1.9/LinearAlgebra/src/adjtrans.jl:295 length(::Union{SparseArrays.FixedSparseVector{Tv, Ti}, SparseArrays.SparseVector{Tv, Ti}} where {Tv, Ti}) @ SparseArrays /builddirs/julia/usr/share/julia/stdlib/v1.9/SparseArrays/src/sparsevector.jl:95 ... ```
1 parent 94ba2dd commit 3b60aa9

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/JuMP.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ LinearAlgebra.adjoint(scalar::AbstractJuMPScalar) = conj(scalar)
10591059
Base.iterate(x::AbstractJuMPScalar) = (x, true)
10601060
Base.iterate(::AbstractJuMPScalar, state) = nothing
10611061
Base.isempty(::AbstractJuMPScalar) = false
1062+
Base.length(::AbstractJuMPScalar) = 1
10621063

10631064
# Check if two arrays of AbstractJuMPScalars are equal. Useful for testing.
10641065
function isequal_canonical(

test/test_variable.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,4 +1601,11 @@ function test_bad_bound_types()
16011601
return
16021602
end
16031603

1604+
function test_variable_length()
1605+
model = Model()
1606+
@variable(model, x)
1607+
@test length(x) == 1
1608+
return
1609+
end
1610+
16041611
end # module TestVariable

0 commit comments

Comments
 (0)