Skip to content

@constfield for const fields? #800

@Seelengrab

Description

@Seelengrab

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions