Extend

Methods are functions that can be called on values of the type they are defined for. You can extend types with the extend keyword. This allows you to add methods to types.
After the extend keyword you have to specify the type you want to extend. In the block after that you can add methods to the type.
Methods are defined like functions inside a extend block, the only difference is that you can use the this keyword inside a method.

Example

extend int { func timesTwo(): int { return this * 2; } } let value: int = 4; let newNumber = value.timesTwo(); // 8

Static Methods

Static methods are methods that can be called on the type itself. Static methods are defined like normal methods, but with the static keyword in front of the func-keyword.

Example

extend int { static func timesTwo(value: int): int { return value * 2; } } let value: int = 4; let newNumber = int::timesTwo(value); // 8

this keyword

Inside a method you can access the value the method was called on with the this keyword. More information about the this keyword can be found here.

Read Only

You can make a method read only by adding the readonly keyword behind the func keyword. This means that the method cannot change the value it was called on.

extend int { func readonly timesTwo(): int { return this * 2; } }

The reason why you would want to make a method read only is that you can call it on a constant value.

Example

extend int { func readonly timesTwo(): int { return this * 2; } } const value = 4; let newNumber = value.timesTwo(); // 8