如何选择私有成员和let绑定?

发布于 2024-12-10 01:45:48 字数 159 浏览 0 评论 0原文

当编写不需要访问同一个类的其他成员的私有方法时,如何在私有成员和let绑定之间进行选择?

  • 我倾向于使用私有成员,因为如果需要的话更容易更改可访问性,但是在做出选择时还需要记住其他方面吗?
  • let 绑定是否编译为私有成员(这只是一种样式选择)?

When writing a private method that has no need to access other members of the same class, how do you choose between private member and let binding?

  • I tend to use private members because it's easier to change accessibility if required, but are there other aspects to keep in mind when making the choice?
  • Are let bindings compiled as private members (making this only a style choice)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒岛晴空 2024-12-17 01:45:48

规范的相关部分是 8.6 节.2。它指出:

用于“let”中声明的值的编译表示形式
类中的绑定是:

  • 对象构造函数的本地值(如果该值是
    不是语法函数,不可变,不用于任何
    函数或成员)。

  • 相应 CLI 类型中的实例字段(如果值为
    不是语法函数,但在某些函数或成员中使用)。

  • 相应 CLI 类型的成员(如果该值为
    句法功能)。

还:

未在以下任一成员中使用的非函数 let 绑定
类型或函数绑定被优化掉并成为以下值:
对于生成的 CLI 构造函数来说是本地的。同样,函数
绑定表示为实例成员。

我更喜欢对私有成员进行 let 绑定,因为它们更具“功能性”,即它们强调“什么”而不是“如何”。编译器负责优化编译形式。

The relevant portion of the spec is section 8.6.2. It states:

The compiled representation used for values declared in “let”
bindings in classes is either:

  • A value that is local to the object constructor (if the value is
    not a syntactic function, is not mutable and is not used in any
    function or member).

  • An instance field in the corresponding CLI type (if the value is
    not a syntactic function, but is used in some function or member).

  • A member of the corresponding CLI type (if the value is a
    syntactic function).

Also:

Non-function let-bindings that are not used in either the members of
a type or a function binding are optimized away and become values that
are local to the resulting CLI constructor. Likewise, function
bindings are represented as instance members.

I prefer let bindings to private members because they're more "functional," i.e., they emphasize "what" over "how." The compiler takes care of the optimal compiled form.

寄居人 2024-12-17 01:45:48

类中的 let 绑定是私有的。我认为 let 和私有 member 之间的主要区别是 let 绑定不能重载,并且使用 name( ) 而不是 this.Name()。因此,我认为这主要是一种风格选择。

let bindings in a class are private. The main difference I think of between let and a private member are that let bindings cannot be overloaded, and are called with name() rather than this.Name(). As such, I think it's a mostly stylistic choice.

霞映澄塘 2024-12-17 01:45:48

let 绑定无法通过类实例访问,但 private 方法 可以。例如:

type A() =
    let someUtil() = "util code"

    member private this.AnotherUtil() = "anotherUtil"

    member private this.DoSomething() =
       let anotherA = A()
       A.someUtil()  // compilation failed
       A.AnotherUtil() // ok

let bindings cannot be accessed through the class instance but private method can. For examples:

type A() =
    let someUtil() = "util code"

    member private this.AnotherUtil() = "anotherUtil"

    member private this.DoSomething() =
       let anotherA = A()
       A.someUtil()  // compilation failed
       A.AnotherUtil() // ok
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文