-
-
Notifications
You must be signed in to change notification settings - Fork 9
Methods
IsaacShelton edited this page Nov 16, 2022
·
3 revisions
Methods are functions whose first argument is named this
and is a pointer to a struct type.
func myMethod(this *MyStruct) {
}
Methods can be called using method calls
subject_value.myMethod(argument1, argument2, argumentN)
When functions are declared inside of struct domains, they are automatically turned into methods and gain an implicit this
parameter.
struct Person (firstname, lastname String) {
func greet {
printf("Hello, my name is %S %S.\n", this.firstname, this.lastname)
}
}
which is equivalent to
struct Person (firstname, lastname String)
func greet(this *Person) {
printf("Hello, my name is %S %S.\n", this.firstname, this.lastname)
}
See structures for more information.
Virtual methods are methods which are dispatched at runtime. They can be defined on classes. See classes for more information.
import basics
class Shape () {
constructor {}
virtual func getArea() float = 0.0
}
class Rectangle extends Shape (w, h float) {
constructor(w, h float){
this.w = w
this.h = h
}
override func getArea() float = this.w * this.h
}
func main {
shape *Shape = new Rectangle(5.0f, 10.0f)
defer delete shape
print(shape.getArea())
}