Hello, I have trouble with understanding why the following code snippet won't compile:
trait Bla {
const SIZE: usize;
fn blabla(&self);
}
struct A;
impl Bla for A {
const SIZE: usize = 20;
fn blabla(&self) {
println!("blablabla {}", Self::SIZE)
}
}
struct B<T: Bla>{
arr: [T; T::SIZE],
}
Can someone please explain why I am getting the following error?
error[E0599]: no associated item named `SIZE` found for type `T` in the current scope
--> src/lib.rs:18:14
|
18 | arr: [T; T::SIZE],
| ^^^^^^^ associated item not found in `T`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `SIZE`, perhaps you need to implement it:
candidate #1: `Bla`
As far as I understand, as long as T has Bla trait bound, I should be able to use T::SIZE, and since SIZE is declared as const, its value should be known at compile time and thus the size of the array (or am I wrong?).
The more explicit version [T; <T as Bla>::SIZE] now says "Bla is not implemented for T". Looks strange to me, possibly the bounds are not "in scope" at that point...
Ah, too bad... Do you have any insight about when this might arrive on nightly? I noticed that the relevant tracking issue has been opened for more than a year.
Last I heard, const generics should be ready (at least in nightly) some time in 2019, perhaps even the first half of the year. Not sure if anything's changed in this regard in the last month or so.
According to #43408 (comment) the error message is "emergent" from the same reason we can't "just" allow this to work right now. That means fixing the error message would be as hard as lifting the limitation in the first place, which is tracked in that issue.