Hello all!
I'm struggling with the specialization. The final goal is to make a bunch of types which can be transformed into each other, depending on the combinations, with compile-time checking. A very simplified example is the following:
#![feature(specialization)]
use std::marker::PhantomData;
trait A {
type Out;
fn f(&self) -> Self::Out;
}
struct S<T> {
data: PhantomData<T>
}
impl<T> A for S<T> {
default type Out = i32;
default fn f(&self) -> Self::Out { 0i32 }
}
impl A for S<i64> {
type Out = f64;
fn f(&self) -> Self::Out { 1.0f64 }
}
playground
This code throws an error, because I can't return prescribed type as associated default, since it can be overwritten in other implementations:
error[E0308]: mismatched types
--> src/lib.rs:15:40
|
15 | default fn f(&self) -> Self::Out { 0i32 }
| --------- ^^^^ expected associated type, found i32
| |
| expected `<S<T> as A>::Out` because of return type
|
= note: expected type `<S<T> as A>::Out`
found type `i32`
But what if I should force the f()
method to always return associated type, no matter how it is overwritten? Is it possible at current stage?