# How to pass on kwargs? - updated

**URL:** <https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782>\
**Category:** New to Julia\
**Tags:** agents\
**Created:** [February 4, 2022, 10:34am UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782 "2022-02-04T10:34:38Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 4, 2022, 10:34am UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/1 "2022-02-04T10:34:39Z")

</div>

Original post below.

It seems the mistake must be somewhere else. I simplified the code to create a running example like this:

```julia
using Agents

mutable struct Agent <: AbstractAgent
    id::Int64
end

function my_model_step!(model)
    ## dummy function
end

function my_agent_step!(agent, model)
    ## dummy function
end

function run_my_model!(model, steps::Integer; kwargs...)
    run!(model, my_agent_step!, my_model_step!, steps, agents_first = false; kwargs...)
end

function data_function(agent::Agent)
    ## dummy function
end

function test_my_model()
    model = ABM(Agent)
    add_agent!(Agent(1), model)
    adata = [data_function]
    # run!(model, my_agent_step!, my_model_step!, 1, agents_first = false; adata)
    run_my_model!(model, 1; adata)
end

test_my_model()
```

and that works.

So, here’s the code that doesn’t work, for a reason that I don’t understand.

```julia
using EconoSim # Package is on Github: https://github.com/HapponomyOrg/EconoSim.jl
using Random
using Agents
using DataFrames

NUM_ACTORS = 1000
SIM_LENGTH = 1000

BASIC_SUMSY = SuMSy(2000, 0, 0.1, 30)

function random_purchase(model, actor::Actor)
    target = random_agent(model)
    transfer_asset!(actor.balance, target.balance, SUMSY_DEP, asset_value(actor.balance, SUMSY_DEP) / 10)
end

function balance(actor)
    return asset_value(actor.balance, SUMSY_DEP)
end

function add_actors(model, behaviors::Vector)
    for i in 1:NUM_ACTORS
        actor = Actor()

        for behavior in behaviors
            add_behavior!(actor, behavior)
        end

        add_agent!(actor, model)
    end
end

function run_sumsy_simulation!(sumsy::SuMSy, behavior)
    model = create_sumsy_model(sumsy)
    add_actors(model, [behavior])    

    adata = [balance]
    data, _ = run_econo_model!(model, SIM_LENGTH; adata)
    # this DOES work: data, _ = run!(model, actor_step!, econo_model_step!, SIM_LENGTH, agents_first = false; adata)

    return data
end

run_sumsy_simulation!(BASIC_SUMSY, random_purchase)
```

The code for run\_econoModel!() is:

```julia
function run_econo_model!(model, steps::Integer; kwargs...)
    run!(model, actor_step!, econo_model_step!, steps, agents_first = false; kwargs...)
end
```

**Original post**

Hi,

I’m implementing a framework build on Agents.jl and want to create a wrapper function for run!() with fewer parameters. In short, I want to do the following:

```julia
function run_my_model!(model, steps::Integer; kwargs...)
  run!(model, my_agent_step!, my_model_step!, steps, agents_first = false; kwargs...)
end
```

All goes well as long as I don’t pass anything after steps. But when I want to substitute:

```julia
adata = [data_function]
run!(model, my_agent_step!, my_model_step!, 10, agents_first = false; adata)
```

which works, with

```julia
adata = [data_function]
run_my_model!(model, 10; adata)
```

which I expected to be the same, I get the following error:

ERROR: MethodError: no method matching run\_my\_model!(::AgentBasedModel{Nothing, Actor, typeof(Agents.Schedulers.fastest), Dict{Symbol, Any}, TaskLocalRNG}, ::Int64; adata=[data\_function])  
Closest candidates are:  
run\_my\_model!(::Any, ::Any, ::Any…) at ~/.julia/packages/EconoSim/mpKhJ/src/models/econo\_model.jl:44 got unsupported keyword argument “adata”  
Stacktrace:  
[1] run\_sumsy\_simulation!(sumsy::SuMSy, behavior::Function)  
@ Main ~/Programming/Visual Studio/MoneySim.jl/src/SuMSySimulation.jl:72  
[2] top-level scope  
@ REPL[7]:1

Actor is a subtype of AbstractActor.  
My problem seems to be with passing the kwargs in the correct way but I can’t figure out what I’m doing wrong.

Thanks in advance,  
Stef

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 4, 2022, 10:47am UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/2 "2022-02-04T10:47:18Z")

</div>

I suspect you have older method definitions interfering here, but it’s hard to tell without a working example, and it would also help to make the code more readable. Please have a look here:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![romainvieme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romainvieme/32/33349_2.png) [@romainvieme](https://discourse.julialang.org/u/romainvieme)\
**Post date:** [February 4, 2022, 12:14pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/3 "2022-02-04T12:14:56Z")

</div>

Hi,  
I guess in your example `adata` is not a real keyword argument, because you don’t give it a value. Did you try this one?

```julia
 run_my_model!(model, 10; adata=[data_function])

```

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [February 4, 2022, 12:26pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/4 "2022-02-04T12:26:44Z")

</div>

That should not be the issue as that synthax is supported for passing kwargs since v1.5  
[https://github.com/JuliaLang/julia/issues/29333](https://github.com/JuliaLang/julia/issues/29333)

---

<div class="post-metadata">

**Author:** ![romainvieme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romainvieme/32/33349_2.png) [@romainvieme](https://discourse.julialang.org/u/romainvieme)\
**Post date:** [February 4, 2022, 1:22pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/5 "2022-02-04T13:22:12Z")

</div>

Yes, I replied too quickly (sorry). Then I can’t do better than @sijo 's answer.  
I tried to reproduce the error, without success.

```julia
function run_my_model!(model, steps::Integer; kwargs...)
    print(kwargs...)
end

```

then

```julia
julia> adata="foo"
"foo"

julia> run_my_model!(:model, 10; adata)
:adata => "foo"

```

No problem here  
Something strange though: the three dots after kwargs appear as the unicode character instead of three ‘dot’ character. I got an error when copy-pasting the function to my terminal.  
‘…’: Unicode U+2026 (category Po: Punctuation, other)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 4, 2022, 1:34pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/6 "2022-02-04T13:34:30Z")

</div>

Firstly, is `adata` a valid keyword argument in `run!`? You haven’t shown `run!`'s definition.

Secondly, it looks like you are misplacing the `;` below:

> [@stefkuypers](#):
>
> ```julia
> function run_my_model!(model, steps::Integer; kwargs…)
> run!(model, my_agent_step!, my_model_step!, steps, agents_first = false; kwargs…)
> end
> 
> ```

The way you are calling `run!` indicates that `agents_first` is a keyword, so either you should move the semicolon, or, if it isn’t a keyword, call `run!` without the parameter name `agents_first`. Either

```julia
run!(model, my_agent_step!, my_model_step!, steps; agents_first = false, kwargs...)
# or
run!(model, my_agent_step!, my_model_step!, steps, false; kwargs...)

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [February 4, 2022, 1:59pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/7 "2022-02-04T13:59:56Z")

</div>

The three dots → Unicode `…` is likely a Discourse thing; it seems to be using a “smart” Markdown processor that does some typographical mapping: for eg., the arrow in the beginning of my reply was actually typed as `->`, but got converted to → by Discourse. (As you can see, it doesn’t do the conversion when text is within a code block - but unfortunately OP didn’t use one, hence the `…`).

---

<div class="post-metadata">

**Author:** ![romainvieme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romainvieme/32/33349_2.png) [@romainvieme](https://discourse.julialang.org/u/romainvieme)\
**Post date:** [February 4, 2022, 2:03pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/8 "2022-02-04T14:03:03Z")

</div>

Didn’t know, thanks !

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 4, 2022, 2:20pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/9 "2022-02-04T14:20:36Z")

</div>

Thanks! I updated my post.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 4, 2022, 2:22pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/10 "2022-02-04T14:22:02Z")

</div>

In Agents.jl run! is defined as follows:  
`
run!(model, agent_step! [, model_step!], n::Integer; kwargs…) → agent_df, model_df
run!(model, agent_step!, model_step!, n::Function; kwargs…) → agent_df, model_df
`

I updated the post since it seems the error is not where I thought it was but I have no clue where to look for it.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 4, 2022, 2:24pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/11 "2022-02-04T14:24:55Z")

</div>

It seems the error is indeed somewhere else but I have no clue where to start looking since calling the wrapper function is what triggers it. Then again, if I simplify the code to the bare minimum, as I did in the updated post, I get no error.

I added the full code (not too big but it does use a self implemented package) in the update. Calling the wrapper function triggers the error while calling run! directly does not for some weird reason.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 4, 2022, 2:31pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/12 "2022-02-04T14:31:42Z")

</div>

There are some serious formatting issues with your OP, you have put in some backticks it seems, but it doesn’t work out correctly. Could you take another look at [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) and try to clean it up?

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 4, 2022, 2:34pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/13 "2022-02-04T14:34:29Z")

</div>

Fixed it. I was using \<code\> instead of [code] blocks.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 4, 2022, 2:40pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/14 "2022-02-04T14:40:29Z")

</div>

I have no clue what I have done but all of a sudden everything runs. Sorry for the inconvenience.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 4, 2022, 2:43pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/15 "2022-02-04T14:43:13Z")

</div>

That’s good. Since I cannot find any definition of `run!`, it’s hard to know what’s happening.

But I still think you should move the semicolon to the front of `agents_first=false`. The semicolon, if used at all, should come before the kwargs.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 4, 2022, 2:51pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/16 "2022-02-04T14:51:02Z")

</div>

Thanks! I’ll keep that in mind.  
The run!() method is defined in the Agents.jl package. In the API it’s defined as follows:

```julia
run!(model, agent_step! [, model_step!], n::Integer; kwargs...) → agent_df, model_df
run!(model, agent_step!, model_step!, n::Function; kwargs...) → agent_df, model_df
```

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [February 4, 2022, 10:20pm UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/17 "2022-02-04T22:20:48Z")

</div>

> [@stefkuypers](#):
>
> ```julia
> function run_econo_model!(model, steps::Integer; kwargs...)
> run!(model, actor_step!, econo_model_step!, steps, agents_first = false; kwargs...)
> end
> 
> ```

As user @DNF said, this is **invalid Julia syntax** and in fact has nothing to do with Agents.jl.

You should write instead:

```julia
function run_econo_model!(model, steps::Integer; kwargs...)
    run!(model, actor_step!, econo_model_step!, steps, false; kwargs...)
end

```

_or_

```julia
function run_econo_model!(model, steps::Integer, agents_first = false; kwargs...)
    run!(model, actor_step!, econo_model_step!, steps, agents_first ; kwargs...)
end

```

because `agents_first` is not a keyword of the `run!` function. It is a positional argument.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 8, 2022, 9:56am UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/18 "2022-02-08T09:56:11Z")

</div>

As far as I understand it, agents\_first is ‘one of the kwargs’ of the run! function, as is adata. You can find the documents for it here: [Tutorial · Agents.jl](https://juliadynamics.github.io/Agents.jl/stable/tutorial/#.-Collecting-data)

agents\_first is specifically mentioned under the section ‘other keywords’.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [February 8, 2022, 10:32am UTC](https://discourse.julialang.org/t/how-to-pass-on-kwargs-updated/75782/19 "2022-02-08T10:32:26Z")

</div>

Oh damn you are right I have forgotten my own function!
