Let's look at the code, generated by F# for simple function:
let map_add valueToAdd xs =
xs |> Seq.map (fun x -> x + valueToAdd)
The generated code for lambda expression (instance of F# functional value) will looks like this:
[Serializable]
internal class map_add@3 : FSharpFunc<int, int> {
public int valueToAdd;
internal map_add@3(int valueToAdd) { this.valueToAdd = valueToAdd; }
public override int Invoke(int x) { return (x + this.valueToAdd); }
}
And look at nearly the same C# code:
using System.Collections.Generic;
using System.Linq;
static class Program {
static IEnumerable<int> SelectAdd(IEnumerable<int> source, int valueToAdd) {
return source.Select(x => x + valueToAdd);
}
}
And the generated code for the C# lambda expression:
[CompilerGenerated]
private sealed class <>c__DisplayClass1 {
public int valueToAdd;
public int <SelectAdd>b__0(int x) { return (x + this.valueToAdd); }
}
So I have some questions:
- Why is F#-generated class not marked as
sealed
? - Why does F#-generated class contain public fields since F# doesn't allow mutable closures?
- Why does F# generated class have a constructor? It may be perfectly initialized with the public fields...
- Why is C#-generated class not marked as
[Serializable]
? Also classes generated for F# sequence expressions also became[Serializable]
and classes for C# iterators do not.