Constructor in Verse
A constructor is a special function that creates an instance of the class that it’s associated with. It can be used to set initial values for the new object.
You can add a constructor for a class by adding the constructor specifier on the function name. Instead of specifying a return type on the function, the function is assigned the class name followed by any initialization of fields. A class can have more than one constructor.
| Verse ``` | |
Verse MakeOtherClass1<constructor>(Arg1 : int) := class1: let: OnePlusArg1 := Arg1 + 1 block: DoSomething(OnePlusArg1) Property1 := OnePlusArg1 block: Expand code Copy full snippet(11 lines long) |
Adding variables and executing code in the constructor: You can execute expressions within a constructor with the block expression, and introduce new variables with the keyword let. |
| Verse ``` |