English 中文(简体)
F#中的通用 d打字?
原标题:generic duck typing in F#?

B. 采用优惠制和成员限制 我无法为已知成员打上uck锁,但如果我这样界定一种通用功能的话:

let rap

带有签字b和“>”的标识;返回价值将成为实施a(即接口)并将电话转.的一个物体。

我在C#中利用反思这样做。 我不禁要问,F#的思考、引言或其他构造是否会更容易。

关于如何做到这一点的任何建议?

EDIT after reading Tims answer I thought I d give a bit more details

我在谈到利用引言帮助时所想的是:

{new IInterface with member x.SayHello() = !!<@ %expr @>}

! 经营人将报价转化为功能,从业者占该方法工作单位的百分比。 我能够将这一表述转化为一种功能(我猜测),但会不知道如何去做。

当然,这只字眼完全是这样,要么因为伊联会是我希望F#思考会有一些手法功能,这样我就能够建造一种基于某种物体和某些功能价值观的类型。

EDIT As an update to Tomas Petricek answer I ll 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)

which is a try at porting "the" textbook example of DCI 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

type sourceContract = 
   abstract decreaseBalance : decimal -> sourceContract

类型。 其结果将是类型源背心的物体,即管道法指源物体上相同名称的方法。

最佳回答
问题回答




相关问题
c# reflection with dynamic class

I need to execute a method "FindAll" in my page. This method returns a list of the object. This is my method that I execute "FindAll". FindAll requires an int and returns an List of these class. ...

Performance overhead of using attributes in .NET

1.. Is there any performance overhead caused by the usage of attributes? Think for a class like: public class MyClass { int Count {get;set;} } where it has 10 attibutes (...

WPF GridView, Display GetType().Name in DisplayMemberBinding

I want include the Type name of each object in my collection from my GridView. I have a collection which has four different types in it that all derive from a base class. Call them, Foo, Bar, Fizz, ...

Testing private method of an abstract class using Reflection

How can I test a private method of an abstract class using reflection (using C#)? I am specifically interested in adapting the code found in this thread. I am aware of the discussion around the ...

Adding Items to ListBox, RadioList, Combobox using reflection

I m trying to add items to a listbox,combobox, radiolist using reflection. The code I have at the moment is as follows: public static Control ConfigureControl(Control control, ControlConfig ctrlconf)...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签