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

Type parameters on statics #10798

Closed
brendanzab opened this issue Dec 4, 2013 · 10 comments
Closed

Type parameters on statics #10798

brendanzab opened this issue Dec 4, 2013 · 10 comments
Labels
A-typesystem Area: The type system

Comments

@brendanzab
Copy link
Member

Would this be possible?

pub enum Reply<V, StopReason> {
    Reply(V),
    Stop(StopReason),
}

pub static Continue<StopReason>: Reply<(), StopReason> = Reply(());

I guess it would be less useful than the C++14 proposal seeing as our constexprs are currently so limited.

@nikomatsakis
Copy link
Contributor

This is...basically identical to writing

fn Continue<S>() -> Reply<(), S> { Reply(()) }

except that you don't need the ().

In general I'd say it's not "possible" in the sense of static things representing a value that is pre-allocated and lives in RO memory.

@nikomatsakis
Copy link
Contributor

Closing as impossible, re-open if you disagree.

@huonw
Copy link
Member

huonw commented Dec 4, 2013

Out of curiousity, could this be implemented by monomorphising statics like we monomorphise functions? i.e. Continue doesn't appear in the executable, but things like Continue<u32> and Continue<Foo> do, if it's used in places where the type parameter is given those types.

@glaebhoerl
Copy link
Contributor

@huonw that's exactly what the C++ proposal does (see link in OP).

I don't know whether there's compelling use cases as of yet -- it took C++ until now to get around to wanting them, and their templates and constexprs are much more sophisticated than Rust's -- but I think it's a reasonable idea.

@huonw
Copy link
Member

huonw commented Dec 5, 2013

One instance where it would be useful would be making a static initialiser for AtomicPtr... not sure if this is worth the trouble of a whole new language feature.

@brendanzab
Copy link
Member Author

@nikomatsakis One issue is you can't pattern match on function calls. :(

@nikomatsakis
Copy link
Contributor

Yes, I suppose we could monomorphize on statics, I'll reopen. I was lacking in vision.

@nikomatsakis nikomatsakis reopened this Dec 6, 2013
@glaebhoerl
Copy link
Contributor

A real use case, not quite the same but probably requiring similar things, would be static var: T in generic functions.

@alexcrichton
Copy link
Member

Issues of this kind now belong in the RFC repo, so closing.

@mbrubeck
Copy link
Contributor

Similar to rust-lang/rfcs#2130

flip1995 pushed a commit to flip1995/rust that referenced this issue May 20, 2023
…r=Jarcho

Don't suggest unnameable types in `box_default`, `let_underscore_untyped`

changelog: [`box_default`], [`let_underscore_untyped`]: Don't suggest unnameable types
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

6 participants