Writing multiple attributes in one line?
Boscop opened this issue · 9 comments
It would be nice to be able to write multiple getset attributes in one line, to reduce verbosity, but when I do (e.g. #[get = "pub", set = "pub"]
) the proc-macro panics..
Can you make it work, please? :)
Yes I'd like this. :)
According to The Reference Manual the grammer for attributes currently doesn't support #[a, b, c]
to denote multiple attributes.
We could work around this possibly by using something like #[foobar(get, set, get_mut]
, but this would be a departure from the current style. So adding this feature would break compatability.
I'm also not sure if the resulting API would be an improvement.
I'm going to mark this help wanted
as I/we need some help figuring out if, and how, this should be done.
I think it would be desirable to allow listing all getters/setters for a field in one line :)
Yeah, I totally agree. Just need to think of the best way to do it! 🤔
Current proposal is a no go
According to rust-lang/rust#55208, this is simple impossible since, even though attribute's syntax was relaxed a bit (a lot), it still requires attributes to correspond to this grammar:
PATH
PATH `(` TOKEN_STREAM `)`
PATH `[` TOKEN_STREAM `]`
PATH `{` TOKEN_STREAM `}`
PATH = TOKEN_TREE
In other words, the first path must be followed (optionally) by one of {
, (
, [
, =
.
Alternative proposal
Support both forms:
// old form
#[get]
#[set]
#[whatever]
// new form
#[getset(get, set, whatever)]
It's pretty easy to disambiguate them, no breaking change is needed. Not to mention that almost every other derive macro out there uses #[name(parameters)]
form.
To be clear, unlike #32 and #35 I'm not proposing to remove or deprecate old style attrs, I'm proposing to add a new alternative syntax. Before you ask, this won't complicate the code much, I am full of resolve to prove it :)
I would be willing to send such a PR.
@CreepySkeleton It sounds good to me. :) Please feel encouraged to open a PR, let me know if I can help with anything!
@Hoverbear could you please review #47 and #48 first so I can stack the upcoming work on top of them?
Yes just give me a day or so, travelling from China. :)
@CreepySkeleton Those looked great. :) Thanks so much for the stuff so far! Sorry about the delays.