February 07

On Wednesday, 7 February 2024 at 13:18:00 UTC, ryuukk_ wrote:

>

There was a DIP for native tuples in D, hopefully we'll get it soon

I just wanted to define DIP for newbs since we're in a new users thread. If they look it up, they will see: Dependency Inversion Principle, Dip programming language, etc..

In the D language context, DIP means: D Improvement Proposal (DIP) which is a formal document that details a potential feature or enhancement to the language,

February 08

On Wednesday, 7 February 2024 at 05:03:09 UTC, Gary Chike wrote:

>

... But overall it's an elegant way to indirectly add names to tuples in D for ergonomic access. Nice find!


Refactored it a bit to have a ready to run and easy to grasp code.

Enjoy!

import std.stdio : writefln;
import std.typecons: tuple;

    // Name a Tuple's fields post hoc by copying the original's fields into a new Tuple.
    template named(names...) {
        auto named(T)(ref auto T t) if (names.length <= T.Types.length) =>
            tuple!names(t.expand[0..names.length]);
    }

    // Define variables corresponding to a Tuple's fields in the current scope,
    // whose identifiers are given by `names`.
    mixin template Unpack(alias t, names...)
    if (names.length <= t.Types.length) {
        static foreach (i, n; names)
            mixin("auto ", n, " = t[i];");
    }

    void main()
    {
    	auto getUser() => tuple("John Doe", 32);
        //
        auto u = getUser().named!("name", "age");
        writefln("%s (%d)", u.name, u.age); // John Doe (32)

        // You could also do this.
        with (getUser().named!("name", "age"))
            writefln("%s (%d)", name, age); // John Doe (32)
        //
        mixin Unpack!(u, "name", "age");
        writefln("%s (%d)", name, age); // John Doe (32)
    }

February 09

On Thursday, 8 February 2024 at 06:09:29 UTC, Menjanahary R. R. wrote:

>

Refactored it a bit to have a ready to run and easy to grasp code.

Enjoy!

Thank you Menjanahary R.! I've saved the code to review and learn from! :>

1 2
Next ›   Last »