C# and F# lambda expressions code generation

前端 未结 2 1728
长发绾君心
长发绾君心 2021-02-05 04:22

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

2条回答
  •  情书的邮戳
    2021-02-05 05:21

    Why is F#-generated class not marked as sealed?

    Because the compiler doesn't (this is in the end a code generation choice.

    Why does F#-generated class contain public fields since F# doesn't allow mutable closures?

    You would need to be able to get to a reference to an instance to modify it. But you can't, so being modifiable doesn't matter. And likely this avoids needing to special case where a mutable is captured in a closure.

    Why does F# generated class have a constructor? It may be perfectly initialized with the public fields...

    Again code generation choice.

    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.

    More code generation choices.

    None of these choices is developer visible (i.e. make no difference to client code) it really makes no difference.

提交回复
热议问题