fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

Type parameter inference from result type of call

Open fridis opened this issue 8 months ago • 0 comments

This is the root cause of #5114: Type parameter inference for bool.ternary ? : does not work (unless the compiler converts this into an inline if statement, which is prevented by previous errors).

A more generic example using one or two value arguments is this:

f(T type, a, b Lazy T) => if 0=1 a else b
g(T type, a Lazy T) => a
_ u8 := f 0 (u8 42)
_ u8 := g 0

which produces errors in both branches since the type parameter is not inferred from the expected result type:

 > ./build/bin/fz test_infer_type_from_result.fz 

/home/fridi/fuzion/clean/fuzion/test_infer_type_from_result.fz:3:11: error 1: Failed to infer actual type parameters
_ u8 := f 0 (u8 42)

In call to 'Lazy', no actual type parameters are given and inference of the type parameters failed.
Expected type parameters: 'T'
Type inference failed for one type parameter 'T'


/home/fridi/fuzion/clean/fuzion/test_infer_type_from_result.fz:3:17: error 2: Failed to infer actual type parameters
_ u8 := f 0 (u8 42)

In call to 'Lazy', no actual type parameters are given and inference of the type parameters failed.
Expected type parameters: 'T'
Type inference failed for one type parameter 'T'


/home/fridi/fuzion/clean/fuzion/test_infer_type_from_result.fz:3:9: error 3: Failed to infer actual type parameters
_ u8 := f 0 (u8 42)

In call to 'f', no actual type parameters are given and inference of the type parameters failed.
Expected type parameters: 'T'
Type inference failed for one type parameter 'T'


/home/fridi/fuzion/clean/fuzion/test_infer_type_from_result.fz:4:1: error 4: Incompatible types in assignment
_ u8 := g 0

assignment to field : '_'
expected formal type: 'u8'
actual type found   : 'i32'
assignable to       : 'i32'
for value assigned  : 'g 0'
To solve this, you could convert the value using + '.as_u8'.

4 errors.

fridis avatar Apr 25 '25 09:04 fridis