You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This code (playground) produces a confusing error message:
pubstructMyType<T>{val:T,}impl<T:ToString>MyType<T>{pubfnnew(val:T) -> Self{Self{
val
}}pubfnnew_from_self(&self,val:u8) -> MyType<u8>{MyType{
val
}}pubfnnew_from_self_generic(&self,val:u8) -> Self{Self{
val
}}}
Method new_from_self compiles and works as expected, but new_from_self_generic produces an error: mismatched types, expected type T, found type u8.
As I understand this, such an error happens because type of T for self will be already deduced at the time of call, and it may be not u8, e.g.:
let my_type_string = MyType::new(String::new());
my_type_string.new_from_self_generic(2);// `T` is `String`, `Self` is `MyType<String>`, not `MyType<u8>`.
However, understanding of this fact took a while, and I had to write some additional code snippets just to understand what compiler wanted from me.
Thus I think that current error message is confusing and should be changed for similar cases at least to something more helpful.
The text was updated successfully, but these errors were encountered:
error[E0308]: mismatched types
--> src/lib.rs:20:13
|
20 | val
| ^^^ expected type parameter, found u8
|
= note: expected type `T`
found type `u8`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
I'll close it given that and the existence of #63711, but if that output is not enough feel free to comment in #63711 and give your current input.
This code (playground) produces a confusing error message:
Method
new_from_self
compiles and works as expected, butnew_from_self_generic
produces an error:mismatched types, expected type T, found type u8
.As I understand this, such an error happens because type of
T
forself
will be already deduced at the time of call, and it may be notu8
, e.g.:However, understanding of this fact took a while, and I had to write some additional code snippets just to understand what compiler wanted from me.
Thus I think that current error message is confusing and should be changed for similar cases at least to something more helpful.
The text was updated successfully, but these errors were encountered: