Skip to content

Commit 0aba776

Browse files
authored
Merge pull request #513 from JuliaOpt/bl/broadcastattr
Broadcast attributes
2 parents f0bce84 + 9b669d2 commit 0aba776

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/attributes.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ abstract type AbstractConstraintAttribute end
3636

3737
const AnyAttribute = Union{AbstractOptimizerAttribute, AbstractModelAttribute, AbstractVariableAttribute, AbstractConstraintAttribute}
3838

39+
@static if VERSION >= v"0.7-"
40+
# This allows to use attributes in broadcast calls without the need to
41+
# embed it in a `Ref`
42+
Base.broadcastable(attribute::AnyAttribute) = Ref(attribute)
43+
end
44+
3945
"""
4046
struct UnsupportedAttribute{AttrType} <: UnsupportedError
4147
attr::AttrType
@@ -183,7 +189,7 @@ function get end
183189
# We want to avoid being too specific in the type arguments to avoid method ambiguity.
184190
# For model, get(::ModelLike, ::AbstractVariableAttribute, ::Vector{VariableIndex}) would not allow
185191
# to define get(::SomeModel, ::AnyAttribute, ::Vector)
186-
get(model::ModelLike, attr::AnyAttribute, idxs::Vector) = get.(model, Ref(attr), idxs)
192+
get(model::ModelLike, attr::AnyAttribute, idxs::Vector) = get.(model, attr, idxs)
187193

188194
function get(model::ModelLike, attr::AnyAttribute, args...)
189195
throw(ArgumentError("ModelLike of type $(typeof(model)) does not support accessing the attribute $attr"))
@@ -264,7 +270,12 @@ set(model, ConstraintFunction(), c, SingleVariable(v1)) # Error
264270
"""
265271
function set end
266272
# See note with get
267-
set(model::ModelLike, attr::Union{AbstractVariableAttribute, AbstractConstraintAttribute}, idxs::Vector, vector_of_values::Vector) = set.(model, Ref(attr), idxs, vector_of_values)
273+
function set(model::ModelLike,
274+
attr::Union{AbstractVariableAttribute,
275+
AbstractConstraintAttribute},
276+
idxs::Vector, vector_of_values::Vector)
277+
return set.(model, attr, idxs, vector_of_values)
278+
end
268279

269280
function set(model::ModelLike, attr::AnyAttribute, args...)
270281
set_fallback_error(model, attr, args...)

0 commit comments

Comments
 (0)