title: generic duck typing in F#?
body: <p>using let inline and member constraints I'll be able to make duck typing for known members but what if I would like to define a generic function like so:</p>
  
  <p>let duckwrapper&lt;'a> duck = ...</p>
  
  <p>with the signature 'b -> 'a and where the returned value would be an object that implemented 'a (which would be an interface) and forwarded the calls to duck.</p>
  
  <p>I've done this in C# using Reflection.Emit but I'm wondering if F# reflection, quotations or other constructs would make it easier.</p>
  
  <p>Any suggestions on how to accomplish this?</p>
  
  <p><strong>EDIT</strong>
  after reading Tims answer I thought I'd give a bit more details</p>
  
  <p>What I was thinking of when I wrote about using quotations to help was something like:</p>
  
  <pre><code>{new IInterface with member x.SayHello() = !!&lt;@ %expr @&gt;}
  </code></pre>
  
  <p>!! being an operator translating the quotation to a function and %expr being the unit of work for the method. I'd be able to translate the expression to a function (I guess) but wouldn't know how to </p>
  
  <p>of course this wouldn't do the trick completely either since IInterface would be 'a which is where I hope F# reflection might have some handy functions so that I could construct a type based on a type object and some function values</p>
  
  <p><strong>EDIT</strong>
  As an update to Tomas Petricek answer I'll give some code to explain my needs</p>
  
  <pre><code>type SourceRole =
     abstract transfer : decimal -&gt; context
  
  and context(sourceAccount:account, destinationAccount) =
     let source = sourceAccount
     let destination = destinationAccount
  
     member self.transfer amount = 
       let sourcePlayer = 
         {new SourceRole with
            member this.transfer amount =
                use scope =  new TransactionScope()
                let source = source.decreaseBalance amount
                let destination = destination.increaseBalance amount
                scope.Complete()
                context(source,destination)
                }
       sourcePlayer.transfer(amount)
  </code></pre>
  
  <p>which is a try at porting "the" textbook example of <a href="http://en.wikipedia.org/wiki/Data,_Context,_and_Interaction" rel="nofollow">DCI</a> in F#.  The source and destination are DCI roles. It's the idea that any data object that adhere's to a specific contract can play those. In this case the contract is simple. source needs a memberfunction called decreaseBalance and destination needs a member function called increaseBalance.
  I can accomplish that for this specific case with let inline and member constraints.
  But I'd like to write a set of functions that given an interface and an object. In this case it could be source (as the object) and</p>
  
  <pre><code>type sourceContract = 
     abstract decreaseBalance : decimal -&gt; sourceContract
  </code></pre>
  
  <p>as the type. The result would be an object of type sourceContract that would pipe method calls to a method with the same name on the source object.</p>