Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 21, 2020 addle 0.1.0 - argument-dependent lookup for UFCS functions | ||||
---|---|---|---|---|
| ||||
Are you tired of D's sane, straightforward scoping rules? Itching for a taste of that old-fashioned C++ madness? Well, itch no more: addle is here to help. addle is a tiny library that implements C++-style argument-dependent lookup (ADL) for D, on an opt-in basis. It lets you extend existing types with UFCS methods, and share those methods seamlessly with code in other modules--no `import` required! Here's a brief example: 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")) ); } Now available on Dub, by "popular demand"! Links: - Documentation: https://addle.dpldocs.info/addle.html - Dub: https://code.dlang.org/packages/addle - Github: https://github.com/pbackus/addle |
June 22, 2020 Re: addle 0.1.0 - argument-dependent lookup for UFCS functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Sunday, 21 June 2020 at 00:06:12 UTC, Paul Backus wrote:
> Now available on Dub, by "popular demand"!
>
> Links:
> - Documentation: https://addle.dpldocs.info/addle.html
> - Dub: https://code.dlang.org/packages/addle
> - Github: https://github.com/pbackus/addle
Cool. Thanks.
|
June 22, 2020 Re: addle 0.1.0 - argument-dependent lookup for UFCS functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Sunday, 21 June 2020 at 00:06:12 UTC, Paul Backus wrote:
> 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"))
> );
> }
As a demonstration of what you can do in D, I love this. Maybe one day I'll even find a use for it. Good work!
--
Simen
|
June 23, 2020 Re: addle 0.1.0 - argument-dependent lookup for UFCS functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Sunday, 21 June 2020 at 00:06:12 UTC, Paul Backus wrote:
> Are you tired of D's sane, straightforward scoping rules? Itching for a taste of that old-fashioned C++ madness? Well, itch no more: addle is here to help.
>
> addle is a tiny library that implements C++-style argument-dependent lookup (ADL) for D, on an opt-in basis. It lets you extend existing types with UFCS methods, and share those methods seamlessly with code in other modules--no `import` required!
>
> Here's a brief example:
>
> 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"))
> );
> }
>
> Now available on Dub, by "popular demand"!
>
> Links:
> - Documentation: https://addle.dpldocs.info/addle.html
> - Dub: https://code.dlang.org/packages/addle
> - Github: https://github.com/pbackus/addle
That's pretty neat.
I wonder if something like that could have been used when checking types in phobos? Probably wouldn't be implemented as I can imagine a performance impact during compilation tho.
|
Copyright © 1999-2021 by the D Language Foundation