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

RFC: syntax for specifying properties for struct fields #35795

Closed
vtjnash opened this issue May 8, 2020 · 5 comments
Closed

RFC: syntax for specifying properties for struct fields #35795

vtjnash opened this issue May 8, 2020 · 5 comments

Comments

@vtjnash
Copy link
Member

vtjnash commented May 8, 2020

We've have some open design questions from the past (#22649, #20415, #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?

@StefanKarpinski
Copy link
Member

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.

@o314
Copy link
Contributor

o314 commented May 15, 2020

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

@quinnj
Copy link
Member

quinnj commented Jun 6, 2020

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

@kmsquire
Copy link
Member

kmsquire commented Jun 6, 2020

As does rust: https://serde.rs/attributes.html

(Although I wouldn’t try to copy the syntax.)

@vtjnash vtjnash closed this as not planned Won't fix, can't repro, duplicate, stale Aug 16, 2024
@o314
Copy link
Contributor

o314 commented Aug 16, 2024

dotnet has attribute , something similar to rust.
it's doable in julia with macro, but more of a meta expr https://docs.julialang.org/en/v1/devdocs/meta/
ok to close bunch and bunch of those issues, but a stabilized public api to attach data via meta expr should be considered - seriously ? - as some point

hot points that need to be handled: composition % ordering, interweaving with the lowerer, data storage & type caps, etc.

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

5 participants