Iterate over macros

I’m using the Parameters package that uses a macro to define parameters. I wonder if there is any way to iterate by changing the struct information. An example:

using Parameters

    @with_kw struct params
        path::String = files_even[1]
        model::String = "dcmst"
        EPS::Float64 = 0.0001
        test::Bool = 1
        plot::Bool = 1
    end

g_params = params()

Here I have an array that has all the paths to instances that I want to test. I would like to iterate through the files_even array by changing the value of the path parameter.
When I implement a for to iterate, it issues the following error:

for i in 1:10
    @with_kw struct params
        path::String = files_even[i]
        model::String = "dcmst"
        EPS::Float64 = 0.0001
        test::Bool = 1
        plot::Bool = 1
    end
end

And the error:

ERROR: syntax: macro definition not allowed inside a local scope

Thanks in advance!

This doesn’t make sense. struct params declares a type. You can only declare a type once.

If you want to declare multiple types in a loop, you can use @eval, but it doesn’t sound like that’s what you want here. You don’t need to declare a new type just to change parameters, just call the constructor to create an instance. (The point of @with_kw is to declare default values of the parameters, but you can override these in instances.)

3 Likes