Hi everyone:
I am playing with generic traits and specialization implementation. The following example works fine, and seems to be the correct syntax:
struct Sys {
}
trait F<T> {
fn f(self) -> T;
}
// Specialization impl
impl F<i32> for Sys {
fn f(self) -> i32 {
3i32
}
}
fn main() {
let s = Sys{};
assert_eq!(3i32, s.f());
}
I somehow tried to write this fragment this other way (the impl
is different):
struct Sys {
}
trait F<T> {
fn f(self) -> T;
}
// Generics would be impl<T> F<T> for Sys
// but specializations are just impl F<i32>
impl<i32> F<i32> for Sys {
fn f(self) -> i32 {
3i32
}
}
fn main() {
let s = Sys{};
assert_eq!(3i32, s.f());
}
and I get the following surprising compiler error:
error: mismatched types [--explain E0308]
--> <anon>:10:9
10 |> 3i32
|> ^^^^ expected type parameter, found i32
note: expected type `i32`
note: found type `i32`
error: aborting due to previous error
I think this is a syntax error, but the compiler does not report it so. Am I missing something here? Is the compiler failing?