Closed
Description
I tried to lazily initialize a static
with an impl trait
value returned by a function (playground):
#![feature(impl_trait_in_bindings)]
static mut TEST: Option<impl core::fmt::Debug> = None;
fn main() {
unsafe { TEST = Some(foo()) }
}
fn foo() -> impl core::fmt::Debug {
0u32
}
This errors with:
error[E0282]: type annotations needed
--> src/main.rs:3:25
|
3 | static mut TEST: Option<impl core::fmt::Debug> = None;
| ^^^^^^^^^^^^^^^^^^^^^ cannot infer type
error[E0308]: mismatched types
--> src/main.rs:6:26
|
6 | unsafe { TEST = Some(foo()) }
| ^^^^^ expected opaque type, found a different opaque type
|
= note: expected type `impl std::fmt::Debug` (opaque type)
found type `impl std::fmt::Debug` (opaque type)
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>
?