Class
Classes in Coco allows you to simplify the handling of complex structures. Each class is made up of fields and methods.
Classes can be declared using the class
keyword followed by the name of the class. Methods can be declared within the class block using the method
keyword followed by the name of the method, input parameters and output parameters.
To access any field or method of the class the value can be called simply using a dot followed by the name of the method or field.
A maximum of 240 methods can be declared on any class excluding the 16 slots reserved for predefined special methods.
memory person = Person{name: "Sam", age: 20, check: 0}
Here person is initialized with a class of type Person with the values as provided
In length = len(personobj)
the total number of fields in the class is stored in the variable length.
Code Syntax
coco Class
class Person:
field name String
field age U64
field check U64
method Add(self):
self.check += 1
endpoint invokable Details(name String, age U64, check U64) -> (nameres String, ageres U64, checkres U64):
memory person Person
person.name = name
person.age = age
person.check = 0
person.Add()
return (nameres: person.name, ageres: person.age, checkres: person.check)
endpoint invokable Literal() -> (length U64, person Person):
memory personobj = Person{name: "Sam", age: 20, check: 0}
length = len(personobj)
yield person personobj