Methods in Chapel
Chapel supports methods defined on record types, which are similar to structs in other languages.
To run the program:
In Chapel, records are value types by default, similar to structs in some other languages. Methods can be defined directly on these record types using the proc recordName.methodName()
syntax.
The area
and perim
methods are defined on the rect
record. In Chapel, the this
keyword is used to refer to the current instance within a method, similar to self
in some other languages.
Chapel doesn’t have a direct equivalent to pointer receivers, but it does have reference types. In the example, we use a shared
keyword to create a reference-counted object, which is similar to using a pointer in some ways.
Chapel’s method calling syntax is the same for both value and reference types, so r.area()
and rp.area()
both work as expected.
Next, we’ll look at Chapel’s mechanism for grouping and naming related sets of methods: interfaces.