Among the 256 method codes, the first 16 (0x0 to 0xF) are reserved for special methods that can be invoked. The special methods are build, throw, emit, join, lt, gt, eq, bool, str, addr, and len.
Special methods can be defined or overridden for classes. E.g., one can define a __len__
of a class or __bool__
and use a class in boolean expressions.
Reserved Method Outputs As these methods are reserved, their output types are predefined. Ensuring your output type matches the method call is vital, as any mismatch will result in failure.
Special Method Signatures
Special Method | Description | Signature |
__build__ | Build | (self X, <any> other) → (<any> value) |
__throw__ | Throw | (self X) → (<any> String) |
__emit__ | Emit | undefined |
__join__ | Join | (self X, <any> X) → (<any> X) |
__lt__ | Less Than | (self X, <any> X) → (<any> Bool) |
__gt__ | Greater Than | (self X, <any> X) → (<any> Bool) |
__eq__ | Equal To | (self X, <any> X) → (<any> Bool) |
__bool__ | Bool | (self X) → (<any> Bool) |
__str__ | String | (self X) → (<any> String) |
__addr__ | Address | (self X) → (<any> Address) |
__len__ | Length | (self X) → (<any> U64) |
These methods must be called in coco using the __ at start and end of the method name.
coco Class
class TooLong:
method __throw__()->(err String):
yield err "string too long"
endpoint invokable TestLen(s String) -> (l U64):
memory l1 = len(s)
memory l2 = s.__len__()
if l2 > 10:
memory tl = TooLong{}
throw tl
if l1 != l2:
memory ts = f"Lengths not equal {l1} != {l2}".__throw__()
throw ts
yield l l1