Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this Inlining for Tuples not work?

Tags:

f#

Why does the inlining not work in this case?

type TupleBuilder () =
     static member inline Cons(a,(b,c)) = (a, b, c)
     static member inline Cons(a,(b,c,d)) = (a, b, c, d)
     static member inline Cons(a,(b,c,d,e)) = (a, b, c, d, e)

 let inline cons h t = TupleBuilder.Cons(h,t)

The call to TupleBuilder.Cons gives me the following compiler error

A unique overload for method 'Cons' could not be determined based on type 
information prior to this program point. A type annotation may be needed. 
Candidates: 
static member TupleBuilder.Cons : a:'a0 * ('a1 * 'a2 * 'a3 * 'a4) -> 'a0 * 'a1 * 'a2 * 'a3 * 'a4, 
static member TupleBuilder.Cons : a:'a0 * ('a1 * 'a2 * 'a3) -> 'a0 * 'a1 * 'a2 * 'a3, 
static member TupleBuilder.Cons : a:'a0 * ('a1 * 'a2) -> 'a0 * 'a1 * 'a2
like image 791
robkuz Avatar asked Oct 04 '17 15:10

robkuz


1 Answers

Inlining alone doesn't delay the overload decision to the call site. You need to add a type A or type B at the overload call.

You can do it easily by using a binary operator in this case:

type TupleBuilder () =
     static member inline ($) (_:TupleBuilder, (b,c))     = fun a -> (a, b, c)
     static member inline ($) (_:TupleBuilder, (b,c,d))   = fun a -> (a, b, c, d)
     static member inline ($) (_:TupleBuilder, (b,c,d,e)) = fun a -> (a, b, c, d, e)

 let inline cons h t = (TupleBuilder() $ t) h


// val inline cons :  h:'a -> t: ^b -> 'c
   when (TupleBuilder or ^b) : (static member ( $ ) : TupleBuilder *  ^b -> 'a -> 'c)

For more inline fun with tuples, have a look at this old blog post.

like image 76
Gus Avatar answered Oct 03 '22 07:10

Gus