2.26. Argument Deduction

It is inconvenient to specify the type arguments for a function. Generally, this is not necessary, as illustrated in the example: Felix deduces the value of the type variables from function arguments.
Start felix section to tut/tutorial/tut-1.26-0.flx[1 /1 ]
     1: #line 2973 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: union list[T] =
     4:   | Cons of T * list[T]
     5:   | Empty
     6: ;
     7: 
     8: struct pair[T,U] =
     9: {
    10:   fst : T;
    11:   snd : U;
    12: }
    13: 
    14: var x = Cons (1,Empty[int]);
    15:   // the [int] in Empty is mandatory
    16: 
    17: x =  Cons(2,x);
    18: x = Cons(3,x);
    19: 
    20: fun f[t] (x:list[t]):list[t] = { return x; }
    21: 
    22: x = f(x);
    23: 
    24: val y = pair(1,2);
    25: print y.fst; print ","; print y.snd; endl;
    26: 
    27: 
    28: // check nested generics now
    29: 
    30: module F[T] {
    31:   fun id1(x:T):T = { return x; }
    32: }
    33: 
    34: print (F[int]::id1 1); endl;
    35: 
    36: module A[T] {
    37:   module B[U] {
    38:     fun id2[V](x:T,y:U,z:V):V*U*T = { return z,y,x; }
    39:   }
    40: }
    41: 
    42: val zyx = (A[int]::B[int]::id2(1,2,3));
    43: print zyx.(0);
    44: print zyx.(1);
    45: print zyx.(2);
    46: endl;
    47: 
    48: // check specialisation
    49: fun idt[T] (x:T):T*T = { return x,x; }
    50: fun idt[T] (x:T*T):T*T = { return x; }
    51: 
    52: val x1 = idt(1); // calls first idt
    53: val x2 = idt(1,2); // calls second idt
    54: print x1.(0); print x1.(1); endl; // print 11
    55: print x2.(0); print x2.(1); endl; // print 12
    56: 
    57: proc pr[T] (x:list[T], pp:T->void) {
    58:   match x with
    59:   | Cons (?i,?t) => { pp i; pr (t,pp); }
    60:   | Empty => { print "Empty"; }
    61:   endmatch
    62:   ;
    63: }
    64: 
    65: proc printint (x:int) { print x; }
    66:   // because we can't use a primitive as a closure yet
    67: 
    68: pr (x,(printint of (int))); endl;
End felix section to tut/tutorial/tut-1.26-0.flx[1]
Start data section to tut/tutorial/tut-1.26-0.expect[1 /1 ]
     1: 1,2
     2: 1
     3: 321
     4: 11
     5: 12
     6: 321Empty
End data section to tut/tutorial/tut-1.26-0.expect[1]