It's because FastFunc isn't a delegate type, you'll need to convert your delegate using the FuncConvert.ToFastFunc.

Cheers,

Rob

By on 9/8/2008 4:42 AM ()

Thanks Rob. Can you write a piece of code how you would convert the function MyModule.f2 in this sample?

By on 9/8/2008 5:11 AM ()

It's actually harder than I thought:

1
int a = Module1.f3(FuncConvert.FuncFromTupled(FuncConvert.ToFastFunc(delegate(Tuple<int, int> ab) { return Module1.f2(ab.Item1, ab.Item2); })), 10, 20);

You need to supply an anonymous delegate that takes a tuple of arguments then wrap this in FuncConvert.ToFastFunc and then FuncConvert.FuncFromTupled.

Cheers,

Rob

By on 9/8/2008 6:12 AM ()

Hi,
just a short addition - if your primary goal is to use the library from C# then it may be worth to design it in a more "C#-friendly" way. Instead of using F# functions in the arguments, you can declare delegate type and using the functions from C# will then be straightforward:

1
2
3
4
5
6
7
 
> type FCallback = delegate of int*int -> int;;
type FCallback =
  delegate of int * int -> int

> let f3 (f:FCallback) a b = f.Invoke(a,b);;
val f3 : FCallback -> int -> int -> int

The C# code could then use the usual style:

1
2
 
int a = Module1.f3(Module1.f2, 10, 20); // method gets converted to the delegate automatically in C#

Hope this helps!
T.

By on 9/8/2008 6:57 AM ()

Thanks a lot.

@tomasp:
- but with your method I can't write my call in F#:

1
  let a = MyModule.f3 MyModule.f2 10 20
By on 9/8/2008 8:21 AM ()

Ok my MyModule is now:

1
2
3
4
5
6
7
let f3 f a b = f a b 

let f1 a b = a * b
let f2 a b = a + b  

let t1 = f3 f1 10 20
let t2 = f3 f2 10 20

and in C#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
            var f2 =
                FuncConvert.FuncFromTupled(
                  FuncConvert.ToFastFunc((Tuple<int, int> ab) => MyModule1.f2(ab.Item1, ab.Item2)));

            var f1 =
                FuncConvert.FuncFromTupled(
                  FuncConvert.ToFastFunc((Tuple<int, int> ab) => MyModule1.f1(ab.Item1, ab.Item2)));

            int a = MyModule1.f3(f2, 10, 20);
            Console.WriteLine(a);

            a = MyModule1.f3(f1, 10, 20);
            Console.WriteLine(a);
            Console.ReadKey();

This works correct, but maybe there is a better solution....

By on 9/8/2008 8:41 AM ()

I think Tomas' solution is going in the right direction, you can call f3 from F# but it's a little more complex:

1
f3 (new FCallback(fun a b -> f2 a b)) 10 20;;

So you'd probably just create a version to be used from F#:

1
let f3' f = f3 (new FCallback(f));;
By on 9/8/2008 8:51 AM ()

Thank you very much.

By on 9/10/2008 5:21 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