module Test =
let f x =
let add a b = a + b //inner function
add x 1
let f2 x =
let add a = a + x //inner function with capture, i.e., closure
add x
let outerAdd a b = a + b
let f3 x =
outerAdd x 1
翻訳先:
[CompilationMapping(SourceConstructFlags.Module)]
public static class Test {
public static int f(int x) {
FSharpFunc> add = new [email protected]();
return FSharpFunc.InvokeFast(add, x, 1);
}
public static int f2(int x) {
FSharpFunc add = new [email protected](x);
return add.Invoke(x);
}
public static int f3(int x) {
return outerAdd(x, 1);
}
[CompilationArgumentCounts(new int[] { 1, 1 })]
public static int outerAdd(int a, int b) {
return (a + b);
}
[Serializable]
internal class [email protected] : OptimizedClosures.FSharpFunc {
internal [email protected]() { }
public override int Invoke(int a, int b) {
return (a + b);
}
}
[Serializable]
internal class [email protected] : FSharpFunc {
public int x;
internal [email protected](int x) {
this.x = x;
}
public override int Invoke(int a) {
return (a + this.x);
}
}
}
内部関数の唯一の追加コストは、 FSharpFunc
のインスタンスを新しくすることです - これはごくわずかです。
非常にパフォーマンスが重視されない限り、私は最も理にかなったスコープ、つまり可能な限り狭いスコープを使用します。