Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign uptype inference for consts/statics #1349
Comments
nrc
added
the
T-lang
label
Oct 31, 2015
This comment has been minimized.
This comment has been minimized.
|
Ambivalent. I’m not using I think type being specified might be better, since it is clear at the first sight what the type of |
This comment has been minimized.
This comment has been minimized.
|
Dunno. You can do this in C++, but it's usually frowned upon (example) (On a related note it would be extra nice to have generic constants inferring their type on their use, like |
This comment has been minimized.
This comment has been minimized.
|
Dup of #296? |
This comment has been minimized.
This comment has been minimized.
|
I think at very least lifetime elisions of references in statics is a good idea. It feels very silly to write |
This comment has been minimized.
This comment has been minimized.
#1623 has now been submitted proposing lifetime elision for statics. |
This comment has been minimized.
This comment has been minimized.
|
With respect to type inference of (There are a few wrinkles, like defaulting, notably for numeric literals, and polymorphic values like |
This comment has been minimized.
This comment has been minimized.
|
I think that doing type inference will risk hurting locality of the code. With elision, the effect is quite limited, whereas with full inference, you can have spooky action at a distance where the errors reported may be quite far away from the actual culprit. |
This comment has been minimized.
This comment has been minimized.
|
Not sure if you are agreeing or disagreeing or not even responding to me at all. :) I agree that full inference would have those negative effects, which is why I think we should have the "literals only" restriction which, I believe, would avoid them. |
This comment has been minimized.
This comment has been minimized.
|
@glaebhoerl so you'd want the full type for B in the following snippet?
|
This comment has been minimized.
This comment has been minimized.
|
Right. (Modulo elision of |
sinkuu
referenced this issue
Nov 23, 2016
Closed
Redundant type specification in const definitions #37950
This comment has been minimized.
This comment has been minimized.
|
What if we inferred types for consts and statics, even based on use, but only within function bodies, leaving top-level type declarations alone? |
This comment has been minimized.
This comment has been minimized.
|
I'd like to see this as well. I regularly find myself having to specify types in |
Centril
referenced this issue
Apr 20, 2018
Closed
determine const type dynamically, remove capitalization requirement. #2404
This comment has been minimized.
This comment has been minimized.
|
I just noticed that #2010 is not linked to anywhere on this issue, which is pretty weird since that appears to be the last big discussion where new things were learned and problems raised that led to the current stalemate. For future readers, the biggest surprise was summarised by niko as follows:
|
hellow554
referenced this issue
Apr 26, 2018
Closed
Rust should support omitting types of constants and static variables #50242
This comment has been minimized.
This comment has been minimized.
Boscop
commented
Apr 26, 2018
•
pub const COL_RED_1 : Col = COL_RED .brightness(BRIGHTNESS_LOW); // const-fn
pub const COL_RED_2 : Col = COL_RED .brightness(BRIGHTNESS_MID);
pub const COL_GREEN_1 : Col = COL_GREEN .brightness(BRIGHTNESS_LOW);
pub const COL_GREEN_2 : Col = COL_GREEN .brightness(BRIGHTNESS_MID);
pub const COL_BLUE_1 : Col = COL_BLUE .brightness(BRIGHTNESS_LOW);
pub const COL_BLUE_2 : Col = COL_BLUE .brightness(BRIGHTNESS_MID);
pub const COL_YELLOW_1: Col = COL_YELLOW.brightness(BRIGHTNESS_LOW);
pub const COL_YELLOW_2: Col = COL_YELLOW.brightness(BRIGHTNESS_MID);The return type of the
pub const PUSH_ENCODER_START: usize = 0;
pub const ROWS_START: usize = PUSH_ENCODER_START + 8;
pub const TOP_GROUP_0: usize = ROWS_START + 16;
pub const TOP_GROUP_1: usize = TOP_GROUP_0 + 1;
pub const TOP_GROUP_2: usize = TOP_GROUP_0 + 2;
pub const TOP_GROUP_3: usize = TOP_GROUP_0 + 3;
pub const STORE: usize = TOP_GROUP_0 + 4;
pub const LEARN: usize = STORE + 1;
pub const EDIT: usize = STORE + 2;
pub const EXIT: usize = STORE + 3;
pub const PRESET_PREV: usize = STORE + 4;
pub const PRESET_NEXT: usize = PRESET_PREV + 1;
pub const BOTTOM_GROUP_0: usize = PRESET_NEXT + 1;
pub const BOTTOM_GROUP_1: usize = BOTTOM_GROUP_0 + 1;
pub const BOTTOM_GROUP_2: usize = BOTTOM_GROUP_0 + 2;
pub const BOTTOM_GROUP_3: usize = BOTTOM_GROUP_0 + 3;
pub static FX_STATES: [usize; 8] = [STORE, LEARN, EDIT, EXIT, BOTTOM_GROUP_0, BOTTOM_GROUP_1, BOTTOM_GROUP_2, BOTTOM_GROUP_3];With type inference it would be enough to annotate the first one as
So I would really like to see type inference for const/static be added (not just function-local). |
This comment has been minimized.
This comment has been minimized.
|
Another one I run into regularly is fixed-sized arrays, where I have to explicitly specify the type including the number of entries. I'd like to omit that number. |
This comment has been minimized.
This comment has been minimized.
|
@Ixrec It seems to me that for |
This comment has been minimized.
This comment has been minimized.
Boscop
commented
Apr 26, 2018
|
@Centril There is also the case where the crate that defines a constant doesn't use it in its own code, but exports it, so it can't be inferred (so it would be a compile-time error "type needs to be specified"). |
This comment has been minimized.
This comment has been minimized.
|
@Boscop That's fine; You can do it for non-pub items then. However, you could potentially hold the type abstract in the crate and so it could work again. |
This comment has been minimized.
This comment has been minimized.
|
@joshtriplett There's a macro for that. It shouldn't be necessary though, I agree. |
This comment has been minimized.
This comment has been minimized.
Boscop
commented
Apr 27, 2018
•
|
The macro is clever but I wouldn't use it because it makes the code harder to read (also for others). I'd prefer to have this built-in.. @Centril It should still infer the type of |
Centril
referenced this issue
Oct 7, 2018
Closed
Consider allowing limited type inference in static declarations #296
This comment has been minimized.
This comment has been minimized.
DoumanAsh
commented
Dec 3, 2018
|
Is there per se anything that stops us from having type inference for static/const regardless of context? |
nrc commentedOct 31, 2015
We shouldn't require types for consts and statics unless necessary.
const FOO = "foo";orstatic bar = 42;should just work. I propose that we try to infer based only on the RHS, i.e., we do not look at uses of consts/statics. Type error if we can't infer based on that. Although this would break the rule that items must be fully annotated, it would make static/const more consistent withlet.