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

Consider allowing limited type inference in static declarations #9346

Closed
sfackler opened this issue Sep 19, 2013 · 3 comments
Closed

Consider allowing limited type inference in static declarations #9346

sfackler opened this issue Sep 19, 2013 · 3 comments
Labels
A-typesystem Area: The type system

Comments

@sfackler
Copy link
Member

Currently, statics must have a type annotation as doing global type inference to resolve the type is a bad idea. However, this leads to some annoying duplication, especially when defining a static struct instance. For example, from extra::base64:

pub static STANDARD: Config =
    Config {char_set: Standard, pad: true, line_length: None};

There's no ambiguity in the type of the RHS, but the LHS must repeat the type anyways.

Would it be possible/reasonable to infer the type of statics from the RHS of the assignment where possible? Things like this would be work:

pub static STANDARD =
    Config {char_set: Standard, pad: true, line_length: None};

pub static HELLO = "hello, world!";

pub static HELLO_BYTES = bytes!("hello, world!");

pub static BAR = 18u8;

but

pub static BAZ = 10;

would not since the type isn't specified (or maybe it would resolve to int?). Untyped integer literals and generic types containing them are actually the only things I can think of that would require a type annotation.

@emberian
Copy link
Member

I think I'd be ok with this, given the current very limited form of constant expressions. It's not possible to change the type of the static without editing its initializer. This is backwards compat, though, so not a priority.

@nikomatsakis
Copy link
Contributor

cc me

@rust-highfive
Copy link
Collaborator

This issue has been moved to the RFCs repo: rust-lang/rfcs#296

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

4 participants