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
Currently there are no mapping between generic types coming from outside a function to inside. I.e. the generics are mapped to the declaration/prototype of the function, but the inferred type is never mapped/checked against the generic type inside the function body.
fn test_fn<T>(x: T) {
x + (3.0 as f32)
}
fn main() -> i32 {
var input: f64 = 5.0
test_fn(input)
return 0
}
Given the example above: The generic type of the function call at line 7 will be inferred to f64 (the type of input). Looking only at the function declaration of test_fn (ignoring the body), it looks like f64 is a valid replacement for T (since it has no restrictions). But if we look in the body of test_fn we can see that T must be of type f32 to be valid.
The two possible options to fix this would be to:
Reject the body of test_fn. Since there are no restrictions/enforcements of what type T can be in a where-clause, it should not be allowed to do addition on a value with the type.
Map the generics from declaration to inside the function body. Using the example above, we would be able to see that a value of type T is added with a f32 which means that T must be of type f32. This is a possible options since there are currently no dynamic dispatch, the generics must be known at compile time anyways.
My preferred option is the first one since it keeps all restrictions enforced on the generics in the function declaration, there are no hidden restrictions inside the function body. It is already possible to enforce traits on the generics in the function declaration, so I feel like that is good enough (could ex. enforce a Addition trait on the generic type to allow addition in the function body).
The text was updated successfully, but these errors were encountered:
Currently there are no mapping between generic types coming from outside a function to inside. I.e. the generics are mapped to the declaration/prototype of the function, but the inferred type is never mapped/checked against the generic type inside the function body.
Given the example above: The generic type of the function call at line 7 will be inferred to
f64
(the type ofinput
). Looking only at the function declaration oftest_fn
(ignoring the body), it looks likef64
is a valid replacement forT
(since it has no restrictions). But if we look in the body oftest_fn
we can see thatT
must be of typef32
to be valid.The two possible options to fix this would be to:
test_fn
. Since there are no restrictions/enforcements of what typeT
can be in a where-clause, it should not be allowed to do addition on a value with the type.T
is added with af32
which means thatT
must be of typef32
. This is a possible options since there are currently no dynamic dispatch, the generics must be known at compile time anyways.My preferred option is the first one since it keeps all restrictions enforced on the generics in the function declaration, there are no hidden restrictions inside the function body. It is already possible to enforce traits on the generics in the function declaration, so I feel like that is good enough (could ex. enforce a
Addition
trait on the generic type to allow addition in the function body).The text was updated successfully, but these errors were encountered: