From 99b6284c9ddf977626f8f1a2b874f9f769a8dac4 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Mon, 27 Nov 2023 00:46:06 +0300 Subject: [PATCH] Provide `Base.IteratorEltype()` / `Base.eltype()` ``` 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 ``` --- src/JuMP.jl | 3 +++ test/test_variable.jl | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/JuMP.jl b/src/JuMP.jl index 9bc01c96cad..659709c6b41 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -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 diff --git a/test/test_variable.jl b/test/test_variable.jl index a3878917bde..53ecff21a99 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -1576,4 +1576,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