We've have some open design questions from the past (https://github.com/JuliaLang/julia/pull/22649, https://github.com/JuliaLang/julia/issues/20415, https://github.com/JuliaLang/julia/issues/9448) of wanting to attach some properties to various fields of an object. Separately, while doing other work (and having need of this now), I ended up stumbling across a syntax that I feel I liked for this. It already parses, but currently has no meaning during lowering. To me, this looks like a parenthetical that describes the field, which is indeed what it is!
So, without further ado, here I show a random example struct (the actual names are nonsense), and apply various keyword attributes to some of the fields (also nonsense, but just to show some possibilities for how this could be used/useful):
mutable struct WrapPointedToThing{T} <: AbstractVector{T}
# align the start to 64-bytes (a cache line) and then offset
# by a pointer (to account for type tag)
# and [only] supports atomic access
rcounter(atomic, align=(64, Sys.WORD_SIZE / 8))::Int
# aligned to 32-bytes (just because)
# and also [only] supports atomic access
wcounter(atomic, align=32)::Int
# constant after initialization
tag(readonly)::Symbol
# constant also, and always a reference pointer
something(noinline, readonly)::T
# just a normal field
lock::ReentrantLock
end
Thoughts? Reactions?
This looks to me like it would make obj.rcounter(atomic)
invoke a method that returns an Int
. I feel like some kind of macro or comment syntax would be good for this.
This is reserved but not implemented
struct A
x::Int = 1
end
Those are not implemented but could be clean too to reach some metatype / datatype
struct A
x::Int => (u, v=(1, 2)) # meta properties thru pairs
end
struct A
x where {u=true, v=(1, 2)} # meta properties thru where clause fields
end
fwiw, go has struct field "tags" (ref) that are convenient for attaching properties to fields, like certain JSON properties (e.g. exclude
, different serialization name/type, etc.)
As does rust: https://serde.rs/attributes.html
(Although I wouldn鈥檛 try to copy the syntax.)
Most helpful comment
This is reserved but not implemented
Those are not implemented but could be clean too to reach some metatype / datatype