Skip to content

TRPL: associated constants #24990

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

Merged
merged 1 commit into from
May 4, 2015

Conversation

steveklabnik
Copy link
Member

No description provided.

@rust-highfive
Copy link
Contributor

r? @huonw

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -0,0 +1,66 @@
% Associated Constants

With the `associated_constants` feature, you can define constants like this:
Copy link
Member

Choose a reason for hiding this comment

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

associated_constants => associated_consts

Copy link
Member Author

Choose a reason for hiding this comment

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

whoops!

@alexcrichton
Copy link
Member

I believe there's also an allowance for associated constants of the form:

pub struct Foo;
impl Foo {
    pub const FOO: u32 = 3;
}

e.g. an inherent constant associated to a type (maybe worth mentioning?)

@alexcrichton
Copy link
Member

cc @quantheory

@steveklabnik steveklabnik force-pushed the doc_associated_constants branch from 8b13f19 to b1d37ae Compare April 30, 2015 18:01
@steveklabnik
Copy link
Member Author

Yeah, worth mentioning for sure. Added, and nits fixed.

with `i32`. It will then use the default value. But, as in `i64`, we can also
add our own definition.

Associated constants don’t have to be with a trait. An `impl` block for a `struct`
Copy link
Contributor

Choose a reason for hiding this comment

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

This phrasing is slightly odd. Did you mean "associated with a trait"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I did.

@quantheory
Copy link
Contributor

LGTM. There is also the ability to define a constant of type Self, just the bare minimum needed for cases like Int::MAX. But it is finicky (e.g. you can use Self in a trait, but have to use the exact type in an impl). I don't know if we want to describe that in the book yet, or wait for the way we deal with type parameters and associated consts to stabilize a bit.

@steveklabnik steveklabnik force-pushed the doc_associated_constants branch from b1d37ae to ba40051 Compare April 30, 2015 21:14
@steveklabnik
Copy link
Member Author

Yeah, let's hold off on the Self stuff.


fn main() {
assert_eq!(1, <i32 as Foo>::ID);
assert_eq!(5, <i64 as Foo>::ID);
Copy link
Member

Choose a reason for hiding this comment

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

As one final nit, I think these are more idiomatically referred to without the angle-bracket syntax, e.g. i32::ID

@alexcrichton
Copy link
Member

r=me with one final nit, thanks @steveklabnik!

@steveklabnik steveklabnik force-pushed the doc_associated_constants branch from ba40051 to 6b465f5 Compare May 2, 2015 15:58
@steveklabnik
Copy link
Member Author

@bors: r=alexcrichton rollup

@bors
Copy link
Collaborator

bors commented May 2, 2015

📌 Commit 6b465f5 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented May 2, 2015

⌛ Testing commit 6b465f5 with merge 2e4fef0...

@bors
Copy link
Collaborator

bors commented May 2, 2015

💔 Test failed - auto-linux-32-nopt-t

@alexcrichton
Copy link
Member

@bors: retry

On Sat, May 2, 2015 at 10:44 AM, bors [email protected] wrote:

[image: 💔] Test failed - auto-linux-32-nopt-t
http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-t/builds/4784


Reply to this email directly or view it on GitHub
#24990 (comment).

bors added a commit that referenced this pull request May 3, 2015
@bors
Copy link
Collaborator

bors commented May 3, 2015

⌛ Testing commit 6b465f5 with merge 77ce30f...

@bors
Copy link
Collaborator

bors commented May 4, 2015

@bors bors merged commit 6b465f5 into rust-lang:master May 4, 2015
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

Successfully merging this pull request may close these issues.

6 participants