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

associated constants can't be used #24915

Closed
tamird opened this issue Apr 28, 2015 · 9 comments
Closed

associated constants can't be used #24915

tamird opened this issue Apr 28, 2015 · 9 comments
Labels
A-associated-items Area: Associated items such as associated types and consts. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@tamird
Copy link
Contributor

tamird commented Apr 28, 2015

https://is.gd/vYwA6X

main.rs:

#![feature(associated_consts)]
struct Foo;

impl Foo {
  const SUP: &'static str = "sup";
}

use Foo::SUP;

fn main() {
  let sup = SUP;
  assert_eq!(sup, Foo::SUP);
}

rustc:

error: unresolved import `Foo::SUP`. Not a module `Foo` [--explain E0432]
 --> <anon>:8:5
8 |> use Foo::SUP;
  |>     ^^^^^^^^

error: aborting due to previous error

I'm not sure if this is specifically not intended, but it seems to me that it would be convenient to mirror the behaviour of enums in this way. cc @quantheory

@steveklabnik steveklabnik added the A-associated-items Area: Associated items such as associated types and consts. label Apr 28, 2015
@quantheory
Copy link
Contributor

This isn't intended to work; associated types and methods can't be used in this way either, so this behavior is consistent with those cases, although I can see how use of associated constants would be convenient. I think that this has been proposed before, but AFAIK there hasn't been an RFC to allow use of associated items yet.

A workaround would be to just declare a copy/alias of the constant in the scope where you wanted to use it:

const SUP: &'static str = Foo::SUP;

This doesn't seem to work for associated types, though, so I should probably file a bug for that if there's not one already.

@tamird
Copy link
Contributor Author

tamird commented Jun 11, 2016

The suggested workaround does work now, but the original issue remains.

@Mark-Simulacrum
Copy link
Member

Do we want this? It feels sort of weird to allow use to import associated constants but not other items from traits/impls. I almost feel like I saw an issue about expanding use to allow this somewhere recently, but I can't find it.

@Mark-Simulacrum Mark-Simulacrum added T-lang Relevant to the language team, which will review and decide on the PR/issue. I-nominated labels Jun 22, 2017
@Mark-Simulacrum
Copy link
Member

Nominating for the lang team. I think it makes sense to discuss this, since it relates to @eddyb's (I believe) thoughts on moving the std::{f32, i32, ...} constants to associated constants and removing the modules. In order to effectively do so though we'd have to either allow importing or have the odd case where we're technically moving to potentially worse-looking code.

@eddyb
Copy link
Member

eddyb commented Jun 22, 2017

We can just deprecate the old modules without removing them.

@Mark-Simulacrum
Copy link
Member

Right, but whereas right now in a module that deals with just u32 I can use std::u32::*; I won't be able to after this feature in any way without running into deprecation warnings.

@joshtriplett
Copy link
Member

This isn't something that was part of the original specification for associated constants. Folks in @rust-lang/lang generally thought this sounded plausible, but it needs an RFC written to specify it.

@aturon aturon closed this as completed Jul 6, 2017
@WiSaGaN
Copy link
Contributor

WiSaGaN commented Jul 25, 2018

This was brought up in #52556.
If we define SECOND as associated const of std::time::Duration, then it would be nice to be able to use
use std::time::Duration::SECOND as S.

@WiSaGaN WiSaGaN mentioned this issue Jul 25, 2018
@petrochenkov
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants