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 upDifferent static variables for different instances of a generic function #2130
Comments
This comment has been minimized.
This comment has been minimized.
|
FWIW the Rust semantics are "if you can write outside a function, putting it inside a block expression will only change where you can refer to it by name and nothing else". So you can embed entire modules in an expression - not only in functions but also array lengths, and they behave the same as outside, except name resolution can get a bit confusing in some cases. |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Aug 27, 2017
•
|
I'd imagine you want polymorphic/generic
Also, the polymorphic/generic
I wonder if this could be merged with #1945 but even if not that might give you ideas about what the RFC should look like. |
This comment has been minimized.
This comment has been minimized.
|
@eddyb Okay, I agree that if we treat fn foo1() {
static A: i64 = 1i64;
println!("{}", A);
}
fn foo2() {
static A: i64 = 2i64;
println!("{}", A);
}, where two @burdges Generic statics indeed seems to solve my problem as you wrote although I have no idea what should happen in the following example: fn foo<T, S>() {
...
struct OnceAndPhandom<T>(Once,PhandomData<T>);
static INIT: OnceAndPhandom<T> = (ONCE_INIT,PhandomData);
INIT.0.call_once(|| {
...
}
fn main() {
foo<i64, usize>();
foo<i64, f64>();
} |
This comment has been minimized.
This comment has been minimized.
|
This sounds like https://github.com/chris-morgan/anymap |
This comment has been minimized.
This comment has been minimized.
All you have to do for consistency is to move anything that can be written outside a function, outside. |
This comment has been minimized.
This comment has been minimized.
sanmai-NL
commented
Oct 16, 2017
|
@hajifkd: Can this issue be closed now? |
This comment has been minimized.
This comment has been minimized.
|
Okay. I'm sorry to bother you. |
hajifkd commentedAug 27, 2017
Currently, different instances of a generic function have an identical static variable.
For example, the following code
calls
println!just once. This is because the entities of the static variableINITare the same for each different instance of the generic functionfoo.(Well, actually I have asked this in stackoverflow, but I only got a workaround to use HashMap essentially. Using dynamical way to dispatch static code sounds a bit uncomfortable. )
I feel this behavior of generic functions and static variables is counter-intuitive since each instances of generic functions have different name in assembly and thus they are different function.
Of course, I agree that changing it loses compatibility and may not be good, so I would like to ask whether there is any room to add a new syntax or something to allow for different instances of a generic function to have different static variables. I feel this is important especially when we "translate" some C++ template code into Rust.