-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Change attribute syntax to @foo #483
Conversation
42dd425
to
45a01bd
Compare
👍 syntax is also used in (at least) Python and Java, and is easy to parse visually. Thanks @sfackler :D |
45a01bd
to
d27643a
Compare
Yeah, I wanna do this. |
This should probably mention as a drawback that I like this idea overall, though. I’ve seen a few newcomers to the language on IRC avoid attributes because they look vaguely like C preprocessor directives. This syntax seems natural to me, has precedent from other languages, and looks nicer than the current weird syntax. 👍 |
I think @foo is a good idea (coming from python/java). But I am not sure about @!foo: Although it matches our current syntax, it is still a little subtle. I wouldn't block on this, but maybe somebody has a better idea? |
I really like this. |
@P1start good point, I added a paragraph. The ambiguity is a bit unfortunate, but I think it'll be a rare enough case in practice that it won't be too much of an issue. |
Hmm, actually, it could cause a real issue for attributes attached to match arms. I don't think there's actually a work around in the currently proposed grammar for this: match (foo, bar) {
@thing
(a, b) => {...}
} Maybe we should continue optionally allowing match (foo, bar) {
@[thing]
(a, b) => {...}
} |
For what it's worth, I actually like how the current attribute syntax looks. I'd much rather be able to apply them in more positions. |
-1. This makes attaching attributes to expressions annoying, and I find
FWIW, if this RFC were to go through and this optional syntax were available, I would prefer to always use the |
cf136d0
to
cc1afb1
Compare
Ah! I was planning on writing up this RFC (sorry, @nikomatsakis!), but I was stymied by precisely the syntax ambiguity that you've used let x = [(1i, 2i)];
match x {
#[lol]
[(a, b)] => (),
} I would really, really, really like to see this RFC land, but I haven't yet thought of the best way to disambiguate this. |
👍 Consistency with other languages is good, all other things being equal. Using |
@bstrie I don't think that's a problem. |
|
||
Note that some attributes which are legal today have no equivalent: | ||
|
||
* `#[deprecated = "foo"]` becomes `@deprecated(reason = "foo")` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just @deprecated("foo")
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the alternatives section for that grammar.
In that case a |
Support for `#[deprecated = "reason"]` style attributes is removed because | ||
`@deprecated = "reason"` is a bit visually confusing since there are no | ||
delimiters wrapping the attribute. There are a couple of alternatives here. | ||
One is to just allow that syntax. It's not grammatically ambiguous, after all. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heartily against this alternative, this form is just noisier and less powerful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @deprecated = "reason"
alternative or the @deprecated("reason")
alternative? I think they're both isomorphic to the current grammar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and I'm heartily against the current #[foo = "bar"]
form as well, which is why I'm so eager to use this opportunity to nix it from the language. :)
Or allow |
@Valloric I think that's a somewhat disingenuous argument; Python lets you define and use a custom function or object. I believe Java lets you attach custom objects to the runtime metadata for introspection purposes. Rust only allows constructs defined by the compiler or standard library; it won't be possible to define these in stable Rust anytime soon. In addition, insofar as I'm aware, neither Python or Java allow inner attributes, only outer ones. Oh, and someone coming from D might be forgiven for believing that these are all just regular keywords that happen to have a Also, C# uses I don't think the consistency argument is very well founded; it's grasping at a relatively minor visual distinction, whilst ignoring deeper semantics. I could make the argument that this is similar to the |
That isn't true. You can attach whatever you want - it just might not do anything (and warn).
I'm hoping to get syntax extensions into the stable language sometime next year. |
@huonw yeah, that's another option. There's a bit of a risk there of syntax extensions messing up because they're looking specifically for a |
@sfackler I think we can solve those implementation details satisfactorily enough for it to not be a worry (e.g. removing |
With a new attribute sigil, the inner attribute is horrible to read in my opinion: |
I would rather have |
@reem Yeah, now that I think about it, I prefer |
Yeah, Rust's attributes were inspired by C#'s attributes IIRC. I have no strong feelings about this landing (I prefer the old syntax but wouldn't mind the new one), but I am deeply skeptical that |
-1. Frankly, motivation is less than compelling. It does look like a personal preference. Especially given that there are ambiguities. I'm convinced that drawbacks are much larger than benefits (which I don't even see, beyond similarity to other languages, extent of which is arguable). |
-1 agree that this is just personal preference, I like the current style |
My personal preference is |
-1 frankly the arguments for this are not compelling enough to justify the hassle, especially since we now have quite a lot of existing Rust projects and this will break pretty much everyone's code. It seems to be just a personal preference and I failed to see how two less keystrokes will improve the usabiliy/productivity in any fundamental way. |
Those advocating square brackets (single, as in C♯, or double, as in C++) should realise the unavoidable ambiguity with array literals once we get to attributes on expressions. For that reason I think it is fair to say that we simply cannot do just that, as we have declined to have a requirement of arbitrary lookahead to resolve ambiguities. When it comes to consistency with other languages, our current Really, if we week to change our syntax for consistency with other languages, removing any that are not syntactically possible for us to adopt (such as the square brackets of C♯/C++), this I feel that going for familiarity and conformity with other languages in this way is of value. 👍. |
I will, however, note that I’m not so comfortable with |
I think this would be a good idea. I like that If we are going to do a change like this, I'm strongly in favor of doing it before 1.0 |
As far as that goes, |
👎 Unlike the macro syntax change |
A large fraction of my attributes are going to be I guess if people really want this I don't mind it. But it means going through the whole library update dance. But unlike every other recent change, this basically breaks everybody, so it's going to take a couple of days to track down all the package authors and ask them to upload new versions to crates.io. Also, if this patch lands today, there's a good chance I won't be able to do much Rust coding over the long weekend in the US. So -1 to a globally breaking change right before a major US holiday. |
+1 for either |
What precisely is it that people see as an advantage in I personally consider |
Submitted #484 as an alternative (keep the existing syntax, just make the brackets optional). |
👎 I like |
I don't think that breakage will be a big deal, given that we can ship a rudimentary tool to fix up code automatically. And this won't be the last time that Rust code will break, given the yet-forthcoming switch to unboxed closures. I really don't care if the sigil is |
"It looks bad in my opinion" is not a good reason to introduce a major breaking change. |
-1 I like the way attributes look currently. They remind me of attributes in F#: [<EntryPoint>]
let main () =
printfn "Hello world" |
@iirelu Familiarity for people coming from other languages is a good reason to introduce a major breaking change. Our current I should also point out that this change has been brewing since 2012 at least, though it was only made possible this year due the removal of the managed pointer syntax. This also would have been done last month had RFC #386 not decided to unfortunately entangle the change with a much more controversial attempt to unify the syntax of attributes and macros. I'm sympathetic to the breakage argument, but not the extent of penalizing all future users and learners of the language in the years to come. See also how we ended up with Make's notorious whitespace syntax. We can provide a tool to make the switch painless for library authors. We should provide such a tool. I would appreciate mention of this being added to the RFC. |
I guess I disagree with the familiarity argument, since I don't think the current syntax is all that different. But this might be my biases talking here, because I don't usually interact with Rust newbies. I would feel much less strongly about the breakage point if something like @steveklabnik's approach on |
As an actual newcomer to the language, with background in perl, ruby, Erlang, clojure, JavaScript, and a smattering of c - there was nothing odd about the current syntax. I did have to understand what it was at all, but that would be true of the @ syntax as well. Honestly, # made sense to me because it was a comment in many other languages, and rust was using it as a 'special kind of comment'. I'm sure I would've adapted to the @ syntax just as well, but the mental mnemonic wouldn't have kicked in. $0.02 from newb land. |
-1. I started learning Rust a few weeks ago, having used C#, C, C++, Java, Dart, and D before, and I've liked Rust's attribute syntax from the beginning. I've always thought that using |
-1 on |
@vadimcn We could use |
OK, I've spent a while looking at Rust code using This change is still probably going to break the Rust web stacks for a couple of days, because the web dependency chain is so deep, and getting everybody to patch things and upload new crates will take a while. |
+1 |
-1 it takes just a few minutes to get familiar with the current syntax. No gain here. |
We discussed this with the Rust team and although most of us like the look of Thanks for writing the RFC! |
As with the enum thing, I can commit to pushing the impl through relatively quickly.
Rendered