I feel really dumb about this one, but I am having a real hard time finding documentation on this.
If I declare a struct like so:
type BuildNumber =
struct
val major : int
val minor : int
val build : int
val revision : int
end
Then how do I make a new instance of the BuildNumber
type?
We create an instance by stating the name of the struct and then add curly brackets containing key: value pairs, where the keys are the names of the fields and the values are the data we want to store in those fields.
Struct Instantiation Using new Keyword An instance of a struct can also be created with the new keyword. It is then possible to assign data values to the data fields using dot notation.
Structure types have value semantics. That is, a variable of a structure type contains an instance of the type. By default, variable values are copied on assignment, passing an argument to a method, and returning a method result. For structure-type variables, an instance of the type is copied.
Using the New Keyword to Create Objects Another way to create an object from a struct is to use a new keyword. While using a new keyword, Golang creates a new object of the type struct and returns its memory location back to the variable.
You use the new
keyword and define a constructor for it.
For example:
type simple =
struct
val A : int
val B : int
new (a: int, b: int) = { A = a; B = b; }
end
let s = new simple(1, 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With