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

Reply via email to