fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

type inference via constraint

Open michaellilltokiwa opened this issue 8 months ago • 1 comments

both examples should just work IMHO:

ex =>

  a(ERROR type, F type : Lazy ERROR) is

  say (a Any _)
  say (a _ (Lazy Any))

/home/sam/playground/test.fz:5:8: error 1: Failed to infer actual type parameters
  say (a Any _)

In call to 'ex.a', no actual type parameters are given and inference of the type parameters failed.
Expected type parameters: 'ERROR, F'
Type inference failed for one type parameter 'F'


/home/sam/playground/test.fz:6:8: error 2: Failed to infer actual type parameters
  say (a _ (Lazy Any))

In call to 'ex.a', no actual type parameters are given and inference of the type parameters failed.
Expected type parameters: 'ERROR, F'
Type inference failed for one type parameter 'ERROR'

2 errors.

michaellilltokiwa avatar Apr 25 '25 09:04 michaellilltokiwa

I agree for the second case, where ERROR must clearly be Any, but not for the first, since F might still be any type that inherits from Lazy. Currently, the type inference is done from actual value arguments only, actual type parameters should also be taken into account. We should support both, type and value arguments, i.e., the following code should work

  b(T type, U type : Sequence T, v U) =>
    say "$T, $U"
  b ["abc"]            # String, array String 
  b _ _ ["abc"]        # String, array String
  b u8 _ [42]          # u8, array u8
  b i8 _ []            # i8, array i8
  b _ (q u32) nil      # u32, q u32
  q(T type) : choice T nil, Sequence T =>
    public redef as_list => 
      match q.this
        v T => v : nil
        nil => nil

fridis avatar Sep 27 '25 11:09 fridis