Now I come to the F # classes, learning about the most important features of this language. Well, the syntax for defining a class is not easy to understand, but some of the basic concepts are now clear to me, but others are not.
1) The first thing I would like to know is just CORRECT / NOT CORRECT. I realized that classes can be defined in two ways:
- Implicit classes. These classes have only one constructor, and in the first lines of the class you must define, using the let binding, all the internal variables that assign them a value.
- Explicit classes. These classes have many constructors. They take the required values โโval, uninitialized values. These values โโMUST BE INITIALIZED in the constructors. If the constructor cannot determine the value for at least one of the private variables defined using the val binding, the compiler goes crazy.
SEARCH CORRECTLY ???
2) I have a problem with understanding the syntax for the constructor in explicit classes. Consider the following:
Here is the first version:
(* COMPILES :) *) type MyType = val myval: int val myother: int (* Constructor *) new (a: int, b: int) = { myval = a; myother = b; }
Here is the second version:
(* COMPILES :) *) type MyType = val myval: int val myother: int (* Constructor *) new (a: int, b: int) = { myval = a (* No semicolon *) myother = b (* No semicolon *) }
Here is the latest version:
(* DOES NOT COMPILE :( *) type MyType = val myval: int val myother: int (* Constructor *) new (a: int, b: int) = myval = a (* Using the normal indent syntax, without {} *) myother = b (* Using the normal indent syntax, without {} *)
I donโt understand why the first two versions are compiled, and the third, using the usual indentation syntax, does not. This problem only occurs in constructors, because on elements I can use indentation syntax
(* COMPILES :) *) type MyType = val myval: int val myother: int (* Constructor *) new (a: int, b: int) = { myval = a (* No semicolon *) myother = b (* No semicolon *) } (* Indentation accepted, no {} to be inserted *) member self.mymember = let myvar = myval myvar + 10
Why do we need a new function (constructor) {} brackets ????? I donโt like it, because it seems that the sequence counts. In addition, my code also compiles when in {} missiles, between one instruction and another, a semicolon is not inserted. WHY????