Methods
Methods in Spice basically are functions / procedures, assigned to a struct. Within methods,
fields of the parent struct can be accessed like this: this.fieldName.
Usage¶
A basic setup with one struct field, as well es a getter and a setter could look like this:
| Spice | |
|---|---|
Output:
| Text Only | |
|---|---|
Method visibility¶
Like functions and struct fields, methods are private by default. Mark a method public to allow code in other
source files to call it:
| Spice | |
|---|---|
A method that is not marked public can only be called from within the same source file.
Methods on generic structs¶
Methods on a generic struct automatically inherit the struct's type parameters — you do not repeat the parameter list on the method:
| Spice | |
|---|---|
When Stack<int> is instantiated, push accepts an int and pop returns an int.
Tip
You can initialize or destroy structs by using constructors and destructors. Read more about those in the respective documentation section.