# \`const\` syntax outside of \`struct\`

**URL:** <https://discourse.julialang.org/t/const-syntax-outside-of-struct/93173>\
**Category:** General Usage\
**Tags:** metaprogramming, struct, agents\
**Created:** [January 18, 2023, 5:15pm UTC](https://discourse.julialang.org/t/const-syntax-outside-of-struct/93173 "2023-01-18T17:15:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![thevolatilebit](https://avatars.discourse-cdn.com/v4/letter/t/cc9497/32.png) [@thevolatilebit](https://discourse.julialang.org/u/thevolatilebit)\
**Post date:** [January 18, 2023, 5:15pm UTC](https://discourse.julialang.org/t/const-syntax-outside-of-struct/93173/1 "2023-01-18T17:15:41Z")

</div>

**Background:**

In Agents.jl, any agent type being made from calling `@agent` is mutable, but with Julia versions \>= 1.8 now we can specify particular fields as being const, which can help bring performance closer to immutable structs (see [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#Mutable-Composite-Types)). It would be nice to let users to specify fields as const if they like, to improve performance.

**Related issue:**

> <https://github.com/JuliaDynamics/Agents.jl/issues/728>
>
> I use a variant of Agents.jl \`@agent\` macro in \[AlgebraicAgents.jl\](https://gith…ub.com/Merck/AlgebraicAgents.jl). @slwu89 recently suggested to me the following idea (\[issue 4, AlgebraicAgents.jl\](https://github.com/Merck/AlgebraicAgents.jl/issues/4)) which clearly applies also to Agents.jl:
> 
> \*Any agent type being made from calling \`@agent\` is mutable, but with Julia versions \>= 1.8 now we can specify particular fields as being const, which can help bring performance closer to immutable structs (see https://docs.julialang.org/en/v1/manual/types/#Mutable-Composite-Types). It would be nice to let users to specify fields as const if they like, to improve performance.\*
> 
> To the best of my knowledge, \`@agent\` macro syntax is not compatible with \`const field\` declarations - at least I have been getting some parsing errors, as we need the \`struct\` wrap.
> 
> As a workaround, I proposed that the syntax of \`@agent\` could change to the following:
> 
> \`\`\`julia
> @agent AnotherAgentType \[OptionalSupertype\] struct YourAgentType{X}
> extra\_property::X
> other\_extra\_property::Int
> const immutable\_property::X
> # etc...
> end
> \`\`\`
> 
> You may review a candidate implementation \[there\](https://github.com/Merck/AlgebraicAgents.jl/blob/8b7d05a462cd1ee8303fecbf77f01e7fa13b7d07/src/agent\_macros.jl#L8).
> 
> Would it be possible to achieve the same functionality without the change of syntax?

**Question:**

Is it possible to embed `const` syntax in `@agent` macro, as follows:

```julia
@agent YourAgentType{X} AnotherAgentType [OptionalSupertype] begin
        extra_property::X
        other_extra_property::Int
        const immutable_property::X # immutable type
        # etc...
end

```

Note that this will not even parse at all:

```julia
ERROR: syntax: expected assignment after "const"

```

It would be possible to rework the `@agent` syntax, to use:

```julia
@agent AnotherAgentType [OptionalSupertype] struct YourAgentType{X}
        extra_property::X
        other_extra_property::Int
        const immutable_property::X
        # etc...
end

```

However, that would be a breaking change, which the authors want to avoid.

@Datseris @slwu89 may find it interesting
