Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow const syntax in the @agent macro #728

Closed
thevolatilebit opened this issue Jan 15, 2023 · 7 comments
Closed

allow const syntax in the @agent macro #728

thevolatilebit opened this issue Jan 15, 2023 · 7 comments

Comments

@thevolatilebit
Copy link

I use a variant of Agents.jl @agent macro in AlgebraicAgents.jl. @slwu89 recently suggested to me the following idea (issue 4, AlgebraicAgents.jl) 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:

@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.

Would it be possible to achieve the same functionality without the change of syntax?

@Datseris
Copy link
Member

Datseris commented Jan 17, 2023

Hm, unfortunately this syntax is a breaking change. But maybe there is a simple way to modify @agent so that it can parse const the way it is now. (I dont know this way at the moment). Have you asked for help in discourse?

Allowing the cost decleration is definitely desirable

@Datseris Datseris changed the title mutability of agent structs allow const syntax in the @agent macro Jan 17, 2023
@thevolatilebit
Copy link
Author

So far I asked in slack - the suggestion was to look at Base.@kwdef, which uses the struct syntax, so this does not really apply here. I will ask on discourse as well.

@thevolatilebit
Copy link
Author

@fbanning
Copy link
Member

This would indeed be very nice. Also to be able to use Base.@kwdef (see #709) would be great. If we start to work on (or possibly rewrite) the @agent macro, then we should consider implementing that at the same time.

@Datseris
Copy link
Member

I've thought about this a lot and I think I will revert the decision of @agent macro being the only way to make agent types, and allow types to be made also directly by the users. Instead @agent macro will be the recommended way. At the moment there are some problems with @agent we do not know how to resolve. If in the future they are resolved, then we can enforce its usage, but for now, it is better to relax it I guess.

@Tortar
Copy link
Member

Tortar commented Jul 14, 2023

With #831 we have a sort of solution also for this one:

use something like

@agent A NoSpaceAgent begin
           f1::Int = 40
           f2::Int
           consts = :f1, :f2
       end

and rework a little the macro, there shouldn't be breaking changes

@Tortar
Copy link
Member

Tortar commented Jul 15, 2023

Actually I think the decision requires some thoughts, we can do it with what I mentioned in the previous comment:

@agent A NoSpaceAgent begin
           f1::Int
           f2::Int
           consts = (:f1, )
       end

or instead we could use something like

@agent A NoSpaceAgent begin
           const_f1::Int # or any other special prefix instead of const_
           f2::Int
       end

The first seems more elegant to me, but then we need to find a suitable more complex regex (or metaprogramming) than in the second case to restructure the result in the struct before interpolation (which means be more careful about edge cases):

@kwdef mutable struct A
           id::Int
           const f1::Int
           f2::Int
       end

thoughts @Datseris ?

No solutions here allows to directly use the const syntax unfortunately, but this is impossible at the moment since that syntax throws errors immediately when not in a struct, which means that some workaround like this seems necessary, I think that having this option would be really cool nonetheless.

I would go with the first way since it seems better syntactically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants