Overload declaration

Grammar§

OverloadDeclaration ::= Attribute* "overload" Identifier (":" Type)? "{" OverloadMember ("," OverloadMember)* ","? "}" OverloadMember ::= IdentExpression | DotExpression ;

Semantics§

An overload declaration allows to create a set of functions that are callable using the same identifier. Overload declarations can contain free functions but also member functions if declared in the same parent as the members. Each function must have an unique type.

function createFromBytes(u8[] values) {return;}
function createFromFile(s8* name)     {return;}

overload create
{
    createFromBytes,
    createFromFile,
}

function main(): s32
{
    const s8* name = "test.txt";
    create(name); // calls createFromFile
    return 0;
}

The members are tried following the lexicographical order, in two passes. Only the second pass tries the member with implicit conversions enabled.

function testF32(f32 value){return;}
function testF64(f64 value){return;}

overload test
{
    testF64,
    testF32
}

function main(): s32
{
    const f32 v = 0.0:f32;
    test(v); // calls testF32
    return 0;
}

An overload declaration can extend an existing overload. This is useful for example to extend overloads defined in libraries.

import rtl.convert;

overload toString : rtl.convert.toString
{
    convertItemtoString
}

struct Item {}

convertItemtoString(var Item item): s8*
{
    return "representation...";
}

Only a single base is allowed as otherwise unrelated sets could be merged.