Skip to content

Extension Functions

Paul T edited this page May 25, 2017 · 2 revisions

This only works for local variables. If this is attempted on a full namespace, the compiler will fail to parse and generate bogus junk.

Extension functions are a way to reduce verbosity. Consider following code:

import std::string;

function main:int argc:int, argv:[[char]]
    str = std::string::new from:"Abc" : std::string;
    len = str.length() : size_t;     # std::string::length of:@str
    a = str.at index:0 : char;       # std::string::at of:@str index:0
    return 0;
end;

On the right side of the comment shows what the call is equivalent to. The restriction is that the functions must be declared in the module with the same name as the type name. Also the are required to have the first parameter called of. Obviously, the type signature of the parameter of has to be the same type name. In the example above, the type signatures of both of are [std::string].

Clone this wiki locally