Not sure I understand what you are trying to do. Are you saying you have a list of functions, and you want to extract the ones whose type is unit -> Result?

In any case, I have been playing with this. Here are some sample functions. "f1" and "f3" match the signature; "f2" and "f4" don't.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
type Result = { Success : bool; Message : string }
type Signature = unit -> Result

let Good (msg:string) = { Success= true; Message= msg }
let Bad (msg:string) = { Success= false; Message= msg }

let f1() = Good "one"
let f2(a) = Bad "two"
let f3() = Good "three"
let f4() = "four"

The following code lists all the functions whose type matches:

<code lang=fsharp>
open Microsoft.FSharp.Reflection

let EqFTypes (t1:System.Type) (t2:System.Type) :bool =
  ((Type.GetInfo t1) = (Type.GetInfo t2))

// Test this works for "f1"
(EqFTypes (f1.GetType()) (type Signature)) |> printfn "%A"
 
let isBoxedType (tm:System.Type) (ob:obj) :bool =
  EqFTypes (ob.GetType()) tm

let Compatible (sig1:System.Type) (funs:obj list) =
  { for f in List.to_seq(funs) when (isBoxedType sig1 f) -> f }

// Boxed in order to list together, because are different types.
let funs = [ box(f1); box(f2); box(f3); box(f4) ]

Compatible (type Signature) funs // Sequence with Signature functions.

let applySM (f:obj) =
  match f with
    | :? (unit -> Result) as f -> f() |> printfn "%A"
    | _ -> failwith "Wrong function type"

Compatible (type Signature) funs |> Seq.iter applySM

== output ==>

1
2
3
4
5
{Success = true;
Message = "one";}

{Success = true;
Message = "three";}

Does this have anything to do with what you are trying to accomplish? ~TMSteve

By on 3/25/2008 8:41 PM ()

Not sure I understand what you are trying to do. Are you saying you have a list of functions, and you want to extract the ones whose type is "unit -> Result"?

Almost. What I'd like to do is create a function that takes any function with type "unit -> Result" as one of its parameters. Or even one that allows a list of functions with type "unit -> Result" as a parameter.

--- Edit ---

After typing this, I tried putting unit in instead of "()", which seems to work. I suppose that makes sense. The actual "type" is unit, the value is ().

Problem solved!

By on 3/26/2008 6:44 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper