Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Func<T, TResult> property in F#

Tags:

c#

f#

I am currently interoperating with C# code from F#.

Is it possible in F# to define a property that in C# looks like below?

private Func<PropertyInfo, bool> OnIsSatisfiedByProperty { get; set; }

(In F# it wouldn't have to be Func<T, TResult> but something equivalent.)

like image 353
Nikos Baxevanis Avatar asked Dec 02 '13 09:12

Nikos Baxevanis


2 Answers

If you need to interop with the type from C#, you def want it to be a Func<T> as opposed to FSharpFunc which has extra metadata such as non-null stipulations [as my earlier comments and @John Palmer's answer ends up being]. Hence the shortest similar actual equivalent you can init with an object-initializer is:

type MyType()=
    member val public OnIsSatisfiedByProperty : Func<PropertyInfo,bool> = null with get,set

or you could go down the route of:

type MyType()=
    [<DefaultValue()>]val mutable OnIsSatisfiedByProperty : Func<System.Reflection.PropertyInfo,bool> 

but my quick messing about leaves me needing to impl explicit set/get methods in that case.

(BTW I knew this answer only due to conincidentally a few mins back, a fixture.Inject( fun () -> !value) wasnt working the same way as fixture.Inject<Func<MyValue>>( fun () -> !value) !)

like image 199
Ruben Bartelink Avatar answered Sep 22 '22 02:09

Ruben Bartelink


I think the equivalent would be something like

type test() = 
        member val private OnIsSatisfiedByProperty:PropertyInfo -> bool = (fun _ -> true) with get,set;;
like image 35
John Palmer Avatar answered Sep 23 '22 02:09

John Palmer