Comparing function types in F# -


the next test fail. call gettype directly function definition, , call gettype within inline function. generated types not equal.

namespace poctests  open fsunit open nunit.framework  module helpers =      let balance ing gas = ing - gas  [<testfixture>]  type ``reflected types`` ()=      [<test>] member x.         ``test type equality inline use`` () =             let inline (=>) f = f.gettype().fullname, f in             let fullname, fval = (=>) helpers.balance in             helpers.balance.gettype().fullname |> should equal (fullname) 

how same type in order "comparable".

when use function value, f# not give guarantees 2 created objects "same". under cover compiler creates new closure object each instance, false result when try this:

balance.gettype().fullname = balance.gettype().fullname 

this intended behavior - when try comparing functions directly, compiler tell functions not satisfy equality constraint , cannot compared:

> let balance ing gas = ing - gas;; val balance : ing:int -> gas:int -> int  > balance = balance;; error fs0001: type '(int -> int -> int)' not support  'equality' constraint because function type 

this means best answer question you're asking cannot done. think comparing function values not idea, perhaps there better answer specific problem if provide more details why want this.

if want perform equality testing on function values, cleanest approach define interface , test ordinary object equality:

type ifunction =    abstract invoke : int * int -> int let wrap f =    { new ifunction       member x.invoke(a, b) = f b } 

now can wrap balance function in interface implementation can compared:

let balance ing gas = ing - gas let f1 = wrap balance let f2 = f1 let f3 = wrap balance f1 = f2 // these 2 same object , equal f1 = f3 // these 2 different instances , not equal 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -