Suppose I've defined the following interface in F#:
type IFoo<'T> =
abstract member DoStuff : 'T -> unit
If I implement this in C# I need the method signature to be:
public void DoStuff<T>(T arg) {...}
What I really want to do is reference FSharp.Core and then use:
public Unit DoStuff<T>(T arg) {...}
This would simplify other code because I wouldn't have to deal with Action vs Func. I'm guessing that there isn't any clean way to achieve this? How about some evil hacks?
Any Boolean function can be implemented using only AND and INVERT gates since the OR function can be generated by a combination of these two gates, as shown in Figure 2.20(a). It follows that these two gates can implement any arbitrary Boolean function and they are said to form a complete set.
There are certain steps involved in it: Step 1: Draw the truth table for the given number of variable function. Step 2: Consider one variable as input and remaining variables as select lines. Step 3: Form a matrix where input lines of MUX are columns and input variable and its compliment are rows.
The OR gate can be implemented by connecting a NOR gate to a single-input NOR gate where single input NOR gate will work as inverter or NOT gate. The AND gate can be implemented by connecting inputs to single input NOR gates acting as inverter which then connect to a NOR gate.
Transformation of Unit
to void
is baked into the compiler. There's a FuncConvert
class in F# Core for converting between FSharpFunc
and Converter
. What about defining a similar class to convert Action<T>
to Func<T, Unit>
?
static class ActionConvert {
private static readonly Unit Unit = MakeUnit();
public static Func<T, Unit> ToFunc<T>(Action<T> action) {
return new Func<T, Unit>(x => { action(x); return Unit; });
}
private static Unit MakeUnit() {
//using reflection because ctor is internal
return (Unit)Activator.CreateInstance(typeof(Unit), true);
}
}
Then you could do
var foo = new Foo<int>();
var func = ActionConvert.ToFunc<int>(foo.DoStuff);
You could probably even forego the Unit
instance and return null
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With