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

Support underscores as constant names #2526

Merged
merged 2 commits into from
Oct 8, 2018

Conversation

joshlf
Copy link
Contributor

@joshlf joshlf commented Aug 20, 2018

Allow the syntax const _: TYPE = VALUE, analogous to let _ = VALUE.

Rendered
Tracking issue

Discussion on internals here.

@Centril Centril added the T-lang Relevant to the language team, which will review and decide on the RFC. label Aug 20, 2018
@ExpHP
Copy link

ExpHP commented Aug 20, 2018

Alternatives section looks barren. I know some ink has been spilt in the past on the alternative of unnamed modules (mod { ... } or mod _ { ... }), which appears to be equivalent in power.

IIRC, one of the primary arguments against that was that hygienic macro will solve the same problem; and that argument applies to this RFC too.

@joshlf
Copy link
Contributor Author

joshlf commented Aug 21, 2018

Alternatives section looks barren. I know some ink has been spilt in the past on the alternative of unnamed modules (mod { ... } or mod _ { ... }), which appears to be equivalent in power.

Good call; I'll add that. Do you know where said ink was spilled so I can link to it?

IIRC, one of the primary arguments against that was that hygienic macro will solve the same problem; and that argument applies to this RFC too.

How would that work?

@Nemo157
Copy link
Member

Nemo157 commented Aug 21, 2018

@joshlf decl_macros create hygienic items, so even if you use a macro generating a named const multiple times in the same scope those consts won't conflict, see this playground for a working assert_impl!.

@nox
Copy link
Contributor

nox commented Aug 21, 2018

Declarative macros are unstable and way bigger than this proposal. That also forces you to use a macro when you could just be doing some compile-time assertions by hand or through a macro_rules macro.

I'm in favour of this proposal.

@mcarton
Copy link
Member

mcarton commented Aug 21, 2018

How about supporting all irrefutable patterns in a similar way to let?

const (foo, bar): (T, U) = computeFooAndBar();

@Centril
Copy link
Contributor

Centril commented Aug 21, 2018

@mcarton see discussion on internals starting here

@joshtriplett
Copy link
Member

This seems reasonable to me.

@upsuper
Copy link

upsuper commented Aug 22, 2018

Another alternative is to support unnamed fn like fn _() { ... } which should provide the same ability.

@joshlf
Copy link
Contributor Author

joshlf commented Aug 23, 2018

Another alternative is to support unnamed fn like fn _() { ... } which should provide the same ability.

Added to the Alternatives section.

@Centril Centril self-assigned this Aug 30, 2018
@Centril
Copy link
Contributor

Centril commented Aug 30, 2018

@rfcbot merge

We discussed the RFC on this week's language team meeting. The general consensus was that we should do this. The proposal seems small in scope, is readily implementable, and it would be useful in some cases (e.g. those mentioned in the motivation, particularly wrt. custom derive macros). It also seems to me that the meaning would be understandable given that let _ = ..; also works.

I would personally like us to go further in the future to generally allow irrefutable patterns so that you can write const (FOO, BAR) = EXPR; so that we align const even more with let, but this is a good first step.

@rfcbot
Copy link
Collaborator

rfcbot commented Aug 30, 2018

Team member @Centril has proposed to merge this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. labels Aug 30, 2018
@Havvy
Copy link
Contributor

Havvy commented Sep 26, 2018

Is there a reason to only allow this for const and not every item type? It feels inconsistent that consts can have them but e.g. structs can not.

@Centril
Copy link
Contributor

Centril commented Sep 26, 2018

@Havvy How would it work for struct and friends? E.g. if we would expand _ to patterns, would it make sense for structs?

I think consts should be primarily consistent with let bindings as much as we can muster to give consts an aura of being bindings. This is why I want to move towards accepting const $pat = $expr; in the future.

@Havvy
Copy link
Contributor

Havvy commented Sep 26, 2018

No, it wouldn't make sense for any item other than const and static to take patterns. Although with static, design decisions have to be made on what it means.

But looking at _ in isolation, struct _ { t: () } would work, even if it's not a pattern. Though that would still be a different usage of _ than either pattern catch-all and type inference. So just allowing patterns on const seems like an okay-to-do-now thing.

@Centril
Copy link
Contributor

Centril commented Sep 26, 2018

@Havvy Right, in the case of struct _ { t: () } the _ is an identifier. I don't see any use cases for this or for enum _ { .. } other than possibly for one-of types (but then you also have to bake in construction, at which point structural records are cleaner...).

@rfcbot rfcbot added the final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. label Sep 27, 2018
@rfcbot
Copy link
Collaborator

rfcbot commented Sep 27, 2018

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. label Sep 27, 2018
@durka
Copy link
Contributor

durka commented Oct 4, 2018

If the only purpose of this is to create an anonymous top-level scope, it kind of seems like a hack.[1] Especially since you have to give the underscore a "type" of () and be sure to return () (you could instead give it a real type and use the return to do a typecheck, like const _: i32 = { hope_this_returns_int() }; but that doesn't work if your types are defined within the const).

If the goal is to have a facility for scopes at the top level, perhaps we should... just allow curly-braced scopes at top level?


[1] If consts could be declared with patterns, this could also be useful in the following code:

const fn foo() -> (i32, i32) { /* ... */ }
const (X, _): (i32, i32) = foo();

But that doesn't work today even with identifiers.


Allow assigning to `_` when defining a new constant. Just like `let _`, this
doesn't introduce any new bindings, but still evaluates the rvalue at compile
time like any other constant.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A guide-level explanation does not include the word "rvalue", IMO. And this doesn't explain what the point of the feature is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a phrase you'd suggest instead of "rvalue"?

The point of the feature is described in the "Motivation" section.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What C and C++ call an rvalue, we call a value expression. See the reference on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we've called it rvalue for some time as well.. see https://github.com/rust-lang/rfcs/blob/master/text/1414-rvalue_static_promotion.md.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to call it rvalue, but we've wisened up since then. Likewise with lvalues (now places).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rkruppe you speak the truth :)

@joshlf
Copy link
Contributor Author

joshlf commented Oct 4, 2018

@durka As you suggest, the idea is that this is the first step on the way towards fully-general patterns like const (X, _): (i32, i32) = foo();. This isn't intended as the final stop.

@rfcbot rfcbot added the finished-final-comment-period The final comment period is finished for this RFC. label Oct 7, 2018
@rfcbot rfcbot removed the final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. label Oct 7, 2018
@rfcbot
Copy link
Collaborator

rfcbot commented Oct 7, 2018

The final comment period, with a disposition to merge, as per the review above, is now complete.

@Centril Centril merged commit cbbc5a7 into rust-lang:master Oct 8, 2018
@Centril
Copy link
Contributor

Centril commented Oct 8, 2018

Huzzah! This RFC has been merged!

Tracking issue: rust-lang/rust#54912

@matthiaskrgr
Copy link
Member

The Rendered url 404s :(

@Centril
Copy link
Contributor

Centril commented Oct 8, 2018

@matthiaskrgr you know it takes a few seconds to change the URL and everything; I'm not a bot =P

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Oct 8, 2018

Ooh, sorry! 😆

@Centril Centril added A-syntax Syntax related proposals & ideas A-const Proposals relating to const items labels Nov 23, 2018
@Centril Centril mentioned this pull request Dec 5, 2018
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jan 6, 2020
Omit underscore constants from rustdoc

Underscore constants from rust-lang/rfcs#2526 / rust-lang#54912 do not correspond to a nameable item and so are never useful in documentation.
<br>

#### Before:

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="https://user-images.githubusercontent.com/1940490/71771409-0427cc80-2eef-11ea-8b7d-d9c74a873e7e.png" width="60%">

#### After:

> Not that.
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jan 7, 2020
Omit underscore constants from rustdoc

Underscore constants from rust-lang/rfcs#2526 / rust-lang#54912 do not correspond to a nameable item and so are never useful in documentation.
<br>

#### Before:

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="https://user-images.githubusercontent.com/1940490/71771409-0427cc80-2eef-11ea-8b7d-d9c74a873e7e.png" width="60%">

#### After:

> Not that.
@joshlf joshlf deleted the joshlf/const-wildcard branch November 10, 2022 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const Proposals relating to const items A-syntax Syntax related proposals & ideas disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this RFC. T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.