Type Extension

“Third party” classes or built-in types that we cannot change otherwise can be extended with “member-like”

In case of conflicts, in-class definitions (inside the class) have priority (and a warning is issued).

Extensions are defined similar to classes, but with the extension keyword.

Extension Methods

Externally Defined Alias

using for members, useful for adapting APIs or providing more descriptive names.

Type Traits

Define member types or traits externally.

Static Variables

Rarely used, but why not.
It is not possible to add non-static member variables, as that would change the size of the class.

extension ContactInfo {
    // External mutable static variable
    static Int numOfCallsToExtensionFunctionX = 0
}

Generic Extensions

Extensions can be parameterized to support

extension<type T, Int N> T[N] {
    using ValueType = T
    
    func length() -> Int {
        return N
    }
    
    func begin() -> T* { return &this[0] }
    func end()   -> T* { return &this[N] }
}

Extension Classes

Allow to create drop-in replacements for existing classes. With no-op conversion similar to operator String&, but in both directions.
Extension classes do not have any additional member variables.

extension class String : std::string {
    using func startsWith = starts_with
}