Syntax error when using @ode_def inside a function

Hi there,

I wanted to use the @ode_def macro from the ParametrizedFunctions package inside some custom function. However, I get a syntax error saying: syntax: "struct" expression not at top level.

Here is a minimum working example:

using ParameterizedFunctions

function mwe()
        f = @ode_def begin
            dx=a
        end a
    a=1j
end

I run it in atom 1.38.1 using julia 1.1.0. on an ubuntu linux.
What is it I am doing wrong?

Thanks for your help
G

Here’s why: the macro expands to define a new type, and that is something you’re not allowed to do within a function:

julia> @macroexpand @ode_def begin
                   dx=a
               end a
quote
    mutable struct var"##402"{var"#43#F", var"#44#J", var"#45#T", var"#46#W", var"#47#Wt", var"#48#PJ", var"#49#TT1", var"#50#TT2"} <: (ParameterizedFunctions.DiffEqBase).AbstractParameterizedFunction{true}
    ...
1 Like