Calling Functions
Within a Coco module, endpoints can call functions to allow for more code reusability.Endpoint callsites cannot be called within a module.
Calling another function requires specifying every argument field name and capturing every output value by its field name.
Field names and captures may be omitted if the variable name, passed as an argument, matches the argument name or the capture matches the target name, so the following statements are both valid:
person = (person) <- InitPerson(name: name, age: age, check: check)
person = InitPerson(name, age, check)
Calling Class Methods
Classes in a Coco module allow you to define a struct within a module. Fields act as parameters of the module. Methods can only be defined on a class. Each method must take self as the first input parameter.
To call a method on a class the a dot must be used after the declared class literal followed by the method name to call the method.
person.Adult()
Adult() method is called the person class object.
coco Calling
class Person:
field name String
field age U64
field check U64
method Adult(self) -> (ok Bool):
yield ok self.age >= 18
func InitPerson(name String, age U64, check U64) -> (person Person):
yield person Person{name: name, age: age}
endpoint invokable Details(name String, age U64, check U64) -> (adult Bool):
memory person = (person) <- InitPerson(name, age, check)
memory isAdult = (ok) <- person.Adult()
yield adult isAdult
endpoint invokable Literal() -> (length U64, person Person):
person = Person{name: "Sam", age: 20, check: 0}
length = len(Person{name: "Sam", age: 20, check: 0})