F#: let mutable vs. ref

后端 未结 5 654
悲&欢浪女
悲&欢浪女 2020-11-29 20:17

First, I acknowledge the possibility that this question could be a duplicate; just let me know.

I\'m curious what the general \"best practice\" is for those situatio

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 20:45

    You may want to take a look at the Mutable Data section in the wikibook.

    For convenience, here are some relevant quotes:

    The mutable keyword is frequently used with record types to create mutable records

    Mutable variables are somewhat limited: mutables are inaccessible outside of the scope of the function where they are defined. Specifically, this means its not possible to reference a mutable in a subfunction of another function.

    Ref cells get around some of the limitations of mutables. In fact, ref cells are very simple data type which wrap up a mutable field in a record type.

    Since ref cells are allocated on the heap, they can be shared across multiple functions

提交回复
热议问题