# Splatting keyword arguments to @variable macro in JuMP

**URL:** <https://discourse.julialang.org/t/splatting-keyword-arguments-to-variable-macro-in-jump/49130>\
**Category:** Optimization (Mathematical)\
**Created:** [October 27, 2020, 3:48pm UTC](https://discourse.julialang.org/t/splatting-keyword-arguments-to-variable-macro-in-jump/49130 "2020-10-27T15:48:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [October 27, 2020, 3:48pm UTC](https://discourse.julialang.org/t/splatting-keyword-arguments-to-variable-macro-in-jump/49130/1 "2020-10-27T15:48:58Z")

</div>

I have a function which returns a particular variable, but defines it differently depending on my model type. I’ve recreated a MWE below.

```julia
using JuMP

function get_z_kwargs(m::Model)
    if m.ext[:model_type] == "binary"
        kwargs = (binary=true)
    elseif m.ext[:model_type] == "integer"
        kwargs = (integer=true, lower_bound=0)
    elseif m.ext[:model_type] == "relaxed"
        kwargs = (lower_bound=0)
    end
end

function make_z_variable(m::Model)
    kwargs = get_z_kwargs(m)
    return z = @variable(m, [1:10], base_name="z", kwargs...)
end

m = Model()
m.ext[:model_type] = "integer"
make_z_variable(m)

```

This produces an error. I can get around this by doing this:

```julia
function get_z_kwargs(m::Model)
    if m.ext[:model_type] == "binary"
        kwargs = (binary=true, integer=false, lower_bound=0)
    elseif m.ext[:model_type] == "integer"
        kwargs = (binary=false, integer=true, lower_bound=0)
    elseif m.ext[:model_type] == "relaxed"
        kwargs = (binary=false, integer=false, lower_bound=0)
    end
end

function make_z_variable(m::Model)
    kwargs = get_z_kwargs(m)
    return z = @variable(m, [1:10], base_name="z",
        binary=kwargs[1], integer=kwargs[2], lower_bound=kwargs[3]
    )
end

make_z_variable(m)

```

But I’m curious to know whether I could adapt my original code to do what I want.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [October 27, 2020, 7:03pm UTC](https://discourse.julialang.org/t/splatting-keyword-arguments-to-variable-macro-in-jump/49130/2 "2020-10-27T19:03:37Z")

</div>

No, splatting keyword arguments doesn’t work with the JuMP macros. Do this instead:

```julia
function apply_model_type(v, m::Model)
    if m.ext[:model_type] == "binary"
        set_binary.(v)
    elseif m.ext[:model_type] == "integer"
        set_integer.(v)
        set_lower_bound.(v, 0)
    elseif m.ext[:model_type] == "relaxed"
        set_lower_bound.(v, 0)
    end
end

function make_z_variable(m::Model)
    z = @variable(m, [1:10], base_name="z")
    apply_model_type(z, m)
    return z
end

```

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [October 28, 2020, 12:57pm UTC](https://discourse.julialang.org/t/splatting-keyword-arguments-to-variable-macro-in-jump/49130/3 "2020-10-28T12:57:00Z")

</div>

Thanks! That’s much nicer 🙂
