young
(rtSwift)
1
For the following test, I wish the compiler error say the actual problem:
"Cannot apply / on or between Numeric"
func fooAAA<T : Numeric>(_ a: T, _ b: T) -> T {
// this results in two compile errors, first is correct, second should not be:
// Binary operator '/' cannot be applied to operands of type 'T' and 'Int' <=== this error is correct
// Binary operator '+' cannot be applied to two 'T' operands <=== !!! but this should not be an error!!!
(a + b) / 2
}
// this version returns void, now the same expression give different compiler error
func fooDDD<T : Numeric>(_ a: T, _ b: T) {
// same expression but give different error:
// Cannot convert value of type 'T' to expected argument type 'Int'
// Replace '(a + b)' with 'Int((a + b))'
// so the compiler give totally different diagnostic for the same expression as above
(a + b) / 2
}
Breaking the expression up produce better error:
let sum = a + b
return sum / 2 // Binary operator '/' cannot be applied to operands of type 'T' and 'Int'
rintaro
(Rintaro Ishizaki)
2
young
(rtSwift)
3
1 Like