-
Notifications
You must be signed in to change notification settings - Fork 13.3k
impl_trait_in_bindings: Use function return type for binding? #60367
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
Comments
Customary warning about Reduced: #![feature(impl_trait_in_bindings)]
static mut TEST: Option<impl core::fmt::Debug> = None;
fn main() {
unsafe {
TEST = Some(0)
}
}
I would expect that this is by design but it would be good to write down a fuller explanation in a less ephemeral location for posterity and for the eventual stabilization report. cc @cramertj @alexreg @oli-obk
Not to my knowledge. You cannot access |
Yes, this is intentional-- items such as fn foo() -> impl Debug { None }
fn bar() {
let mut x = foo();
x = Some(0);
} which we obviously can't make work |
Thanks a lot for clarifying! Just to make sure I understood it right: It can't work because it would require global type inference for statics, function types, etc, but we only do local type inference by design so that e.g. the type of a function does not depend on how it's used. Is that roughly correct? So it seems that lazily initializing a |
I did some more digging and found out that I want to use the existential types feature for this (playground): #![feature(existential_type)]
existential type Debuggable: core::fmt::Debug;
static mut TEST: Option<Debuggable> = None;
fn main() {
unsafe { TEST = Some(foo()) }
}
fn foo() -> Debuggable {
0u32
} The example currently ICEs, but I think it is the way to solve the problem. So given that impl trait works as intended, I'm going to close this issue. Thanks again for the explanation!
I'm aware of that, thanks! I just used it to get a small example. |
I tried to lazily initialize a
static
with animpl trait
value returned by a function (playground):This errors with:
So it can't infer a type that is assigned later. Is this a fundamental limitation or just an incompleteness of the current implementation?
Also, is there a way to name the return type of an function so that I can do e.g.
static mut TEST: Option<foo::RETURN_TYPE>
?The text was updated successfully, but these errors were encountered: