addle

Argument-dependent lookup for extension methods.

addle lets you extend types with UFCS methods, and share those methods seamlessly with code in other modules.

Members

Structs

Extended
struct Extended(T, string context = __MODULE__)

A wrapper that allows extended methods to be called as though they were regular methods.

Templates

extended
template extended(string context = __MODULE__)

Creates an Extended wrapper around an object.

extendedMethod
template extendedMethod(string method, string context = __MODULE__)

Extends a method to include UFCS functions in the calling context.

Examples

import addle;
import std.range;

// Import a type from another module
import mylib: MyStruct;

// Define range primitives for MyStruct
bool empty(MyStruct a) { return false; }
string front(MyStruct a) { return "ok"; }
void popFront(MyStruct a) {}

// MyStruct isn't considered an input range, because
// std.range can't see our UFCS methods.
static assert(isInputRange!MyStruct == false);

// ...but extending it makes those methods visible.
static assert(isInputRange!(Extended!MyStruct));

void main()
{
    import std.range: take, only;
    import std.algorithm: equal;

    MyStruct myStruct;

    // Now we can use all of the standard range algorithms
    assert(
        myStruct.extended
        .take(3)
        .equal(only("ok", "ok", "ok"))
    );
}

Meta

Authors

Paul Backus