我什么时候应该使用 let、成员 val 和成员 this.? [英] When should I use let, member val and member this.?
问题描述
F# 有许多不同的方法来定义类型中的变量/成员.什么时候应该在F#中使用let
、member val
和member this.
,它们之间有什么区别?静态成员和可变成员怎么样?
F# has many different ways to define variables/members in types. When should I use let
, member val
and member this.
in F#, and what is the difference between them? How about static and mutable members?
推荐答案
@meziantou 的回答已经很好地概述了选项(以及它们的行为方式不同),所以让我只给出一个简短的摘要或列表建议:
The answer from @meziantou already gives a nice overview of the options (and how they behave differently), so let me just give a brief summary, or list of recommendations:
使用
let
或let mutable
如果你想定义一个只在类型中可见的本地值(本质上是一个private
> 字段或private
函数).在顶级模块中,这些是公开访问的,并且评估一次.let mutable
在模块级别创建一个没有支持值的可写字段.
Use
let
orlet mutable
if you want to define a local value that is visible only within the type (essentially aprivate
field or aprivate
function). Inside a module at top-level, these are publicly accessible and evaluated once.let mutable
at module level creates a single writable field with no backing value.
你可以使用val
来创建一个自动属性,它是member val Foo = .. with get
的缩写.在 F# 中,这被视为一个字段,但它在内部实现为带有支持字段的 get-property 以防止突变.
You can use val
to create an auto-property, it is short for member val Foo = .. with get
. From F# this is seen as a field, but it's internally implemented as a get-property with a backing field to prevent mutation.
您可以使用 val mutable
来定义公共字段,但除非您确实需要公共字段(例如某些 .NET 库可能需要具有这种结构的类型),否则我不建议这样做).
You can use val mutable
to define a public field, but I wouldn't recommend this unless you actually need a public field (e.g. some .NET library may require types with this structure).
使用 member x.Foo = ...
是从类型公开(只读)状态的最佳方式.大多数 F# 类型都是不可变的,因此这可能是最常见的公共成员.它是仅获取实例属性的缩写.
Using member x.Foo = ...
is the best way to expose (read-only) state from a type. Most F# types are immutable, so this is perhaps the most common public member. It is short for a get-only instance property.
使用 member x.Foo with get() = .. and set(value) ...
当您需要使用自己的自定义代码创建 get/set 属性时很有用在 gettor 和 settor 中.当您创建可变对象时,这有时很有用.
Using member x.Foo with get() = .. and set(value) ...
is useful when you need to create a get/set property with your own custom code in the gettor and settor. This is sometimes useful when you're creating a mutable object.
使用 member val Foo = ... with get, set
与 C# 中的自动实现属性基本相同.如果您需要一个带有 getter 和 setter 的可变属性,只需读取/写入可变支持字段,这将非常有用.
Using member val Foo = ... with get, set
is basically the same thing as auto-implemented properties in C#. This is useful if you need a mutable property with a getter and setter that just reads/writes a mutable backing field.
在类型上使用 static let
创建一个静态(类级别)只读字段,它在内部创建一个带有支持字段的属性.将 static mutable let ...
用于没有支持字段的读/写静态字段.
Using static let
on a type creates a static (class-level) read-only field, which internally creates a property with a backing field. Use static mutable let ...
for a read/write static field without a backing field.
使用 static val mutable private
创建一个带有支持字段的静态读/写自动属性,它不能是公共的.
Using static val mutable private
creates a static read/write auto-property with a backing field, it cannot be public.
这篇关于我什么时候应该使用 let、成员 val 和成员 this.?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!