我什么时候应该使用let,成员val和成员this. [英] When should I use let, member val and member this.?

查看:101
本文介绍了我什么时候应该使用let,成员val和成员this.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

F#具有许多不同的方式来定义类型中的变量/成员.什么时候应该在F#中使用letmember valmember 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:

  • 如果要定义仅在类型(本质上是private字段或private函数)内可见的局部值,请使用letlet mutable.在顶层模块中,这些是可公开访问的,并且可以评估一次. let mutable在模块级别创建一个没有后备值的可写字段.

  • Use let or let mutable if you want to define a local value that is visible only within the type (essentially a private field or a private 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) ...很有用.在创建可变对象时,这有时很有用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆