How do I define a structure inside a function?

Yes. It’s says so. “not at top level”. You can however run top level stuff inside a function with eval or @eval:

function f()
    @eval struct Foo
        a::Int
    end
end

and even create it with you own name

function g(name)
    @eval struct $(Symbol(name))
        a::Int
    end
end
g("MyStruct")
3 Likes