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

@constfield for const fields? #800

Open
Seelengrab opened this issue Jul 15, 2023 · 4 comments
Open

@constfield for const fields? #800

Seelengrab opened this issue Jul 15, 2023 · 4 comments

Comments

@Seelengrab
Copy link

Seelengrab commented Jul 15, 2023

Hi!

I have this in one of my packages:

"""
    @constfield foo::Int

A macro providing compatibility for `const` fields in mutable structs.
Gives a `const` `Expr` if supported, otherwise is a noop and just returns the field.
"""
macro constfield(ex::Expr)
        (ex.head == Symbol("::") && length(ex.args) == 2) || throw(ArgumentError("`@constfield` only supports expressions of the form `field::Type`!"))
    ex = esc(ex)
    if VERSION < v"1.8"
        ex
    else
        Expr(:const, ex)
    end
end

and wanted to ask whether there's interest to have this macro in Compat? It's used like this:

struct Foo
     @constfield foo::Int
end

The idea is to have a noop (if unsupported) replacement for const foo::Int in struct definitions.

@Seelengrab
Copy link
Author

Come to think of it, the argument checking out so allow raw symbols as well 🤔 So this additional version would be needed too:

macro constfield(ex::Symbol)
    ex = esc(ex)
    if VERSION < v"1.8"
        ex
    else
        Expr(:const, ex)
    end
end

@MilesCranmer
Copy link
Member

I wonder if this could be handled with Compat.@compat whenever it wraps a struct?

@martinholters
Copy link
Member

I wonder if this could be handled with Compat.@compat whenever it wraps a struct?

That would be nice, but doesn't work because const fields don't even parse:

julia> :(mutable struct Foo
       const bar
       end)
ERROR: syntax: expected assignment after "const"

(That's julia v1.6.) So there's no way of getting them into the argument of @compat.

@Seelengrab
Copy link
Author

Yeah, that's why I ultimately ended up with the @const-per-field macro that's a no-op on <1.8.

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

3 participants