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

1 import addle;
2 import std.range;
3 
4 // Import a type from another module
5 import mylib: MyStruct;
6 
7 // Define range primitives for MyStruct
8 bool empty(MyStruct a) { return false; }
9 string front(MyStruct a) { return "ok"; }
10 void popFront(MyStruct a) {}
11 
12 // MyStruct isn't considered an input range, because
13 // std.range can't see our UFCS methods.
14 static assert(isInputRange!MyStruct == false);
15 
16 // ...but extending it makes those methods visible.
17 static assert(isInputRange!(Extended!MyStruct));
18 
19 void main()
20 {
21     import std.range: take, only;
22     import std.algorithm: equal;
23 
24     MyStruct myStruct;
25 
26     // Now we can use all of the standard range algorithms
27     assert(
28         myStruct.extended
29         .take(3)
30         .equal(only("ok", "ok", "ok"))
31     );
32 }

Meta

Authors

Paul Backus