How to choose between a private member and bind?

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

  • I usually use private members because it is easier to change accessibility if necessary, but are there other aspects to consider when choosing?
  • Allowed bindings compiled as private members (which makes this choice only for style)?
+6
source share
3 answers

The relevant part of the specification is section 8.6.2 . It states:

The compiled view used for values ​​declared in "let" bindings in classes:

  • A value that is local to the constructor of the object (if the value is not a syntax function, it 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 syntax function, but is used in some function or member).

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

also:

Non-functional let-bindings that are not used in any element type or function binding are optimized and become values ​​that are local to the created CLI constructor. Similarly, the binding function is represented as instance members.

I prefer let bindings to private members because they are more "functional", that is, they emphasize "what" over "how." The compiler will take care of the optimal compiled form.

+10
source

let class bindings are private. The main difference between let and private member is that let bindings cannot be overloaded and are called with name() , not this.Name() . As such, I consider this mainly a stylistic choice.

+5
source

let bindings cannot be obtained through an instance of the class, but private method can. 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 
0
source

Source: https://habr.com/ru/post/899361/


All Articles