using let inline and member restrictions, I can do duck printing for famous members, but what if I wanted to define such a generic function:
let duckwrapper <'a> duck = ...
with the signature 'b β' a and where the return value will be the object that implemented 'a (which would be an interface) and redirected calls to the duck.
I did this in C # using Reflection.Emit, but I'm wondering if F # will make reflection, quotes, or other constructs easier.
Any suggestions on how to do this?
EDIT
after reading Timβs answer I thought I would give a little more information
What I thought about when I wrote about using quotes for help was something like this:
{new IInterface with member x.SayHello() = !!<@ %expr @>}
!! which is an operator that translates a quote into a function, and% expr is the unit of work for the method. I could translate the expression into a function (I think), but didn't know how
Of course, this would not do the trick completely, since IInterface would be "a, where I hope that the F # reflection can have some convenient functions so that I can build the type based on the type object and some function values
EDIT
How to update Tomas Petricek answer, I will give some code to explain my needs.
type SourceRole =
abstract transfer : decimal -> 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)
" DCI F #. DCI. , , , . . -, reduceBalance, -, boostBalance.
let inline .
, . ( )
type sourceContract =
abstract decreaseBalance : decimal -> sourceContract
. sourceContract, .