Skip to content

Commit

Permalink
Provide Base.IteratorEltype() / Base.eltype()
Browse files Browse the repository at this point in the history
```
julia> euclidean(a, b)
ERROR: MethodError: no method matching oneunit(::Type{Any})

Closest candidates are:
  oneunit(::Type{Union{Missing, T}}) where T
   @ Base missing.jl:105
  oneunit(::Type{T}) where T
   @ Base number.jl:370
  oneunit(::T) where T
   @ Base number.jl:369
  ...

Stacktrace:
 [1] oneunit(#unused#::Type{Any})
   @ Base ./missing.jl:106
 [2] _eval_start(d::Euclidean, #unused#::Type{Any}, #unused#::Type{Any}, #unused#::Nothing)
   @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:320
 [3] _eval_start(d::Euclidean, #unused#::Type{Any}, #unused#::Type{Any})
   @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:318
 [4] eval_start(d::Euclidean, a::VariableRef, b::VariableRef)
   @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:317
 [5] _evaluate(d::Euclidean, a::VariableRef, b::VariableRef, #unused#::Nothing)
   @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:236
 [6] (::Euclidean)(a::VariableRef, b::VariableRef)
   @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:328
 [7] top-level scope
   @ REPL[10]:1

```
  • Loading branch information
LebedevRI committed Nov 26, 2023
1 parent 3b60aa9 commit bc09f55
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,9 @@ function owner_model end
Base.ndims(::Type{<:AbstractJuMPScalar}) = 0
Base.ndims(::AbstractJuMPScalar) = 0

Base.IteratorEltype(::Type{<:AbstractJuMPScalar}) = Base.HasEltype()
Base.eltype(::Type{T}) where {T<:AbstractJuMPScalar} = T

# These are required to create symmetric containers of AbstractJuMPScalars.
LinearAlgebra.symmetric_type(::Type{T}) where {T<:AbstractJuMPScalar} = T
LinearAlgebra.hermitian_type(::Type{T}) where {T<:AbstractJuMPScalar} = T
Expand Down
8 changes: 8 additions & 0 deletions test/test_variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1608,4 +1608,12 @@ function test_variable_length()
return
end

function test_variable_eltype()
model = Model()
@variable(model, x)
@test Base.IteratorEltype(x) == Base.HasEltype()
@test Base.eltype(typeof(x)) == typeof(x)
return
end

end # module TestVariable

0 comments on commit bc09f55

Please sign in to comment.