I want to create my own Tuple type
Paul Backus
snarwin at gmail.com
Mon Jan 11 14:03:39 UTC 2021
On Monday, 11 January 2021 at 09:42:39 UTC, Ola Fosheim Grøstad
wrote:
> On Monday, 11 January 2021 at 05:59:03 UTC, Paul Backus wrote:
>> You can just fall back to `alias expand this` like Phobos's
>> Tuple does in this case. No compiler modification needed.
>
> I though maybe it would be nice in general to be able to create
> static indexed type by having a special field name pattern, but
> I will have a another look at staticMap (I don't really want
> the full staticMap into object.d though).
I have no idea why you're bringing up staticMap here. The
simplest way to create a tuple's fields is to use type sequence
instantiation [1], like Phobos does:
struct Tuple(Types...)
{
Types expand;
// ...
}
You can then access the individual fields with `expand[i]`, where
`i` is any compile-time evaluable integer expression. Adding
`alias expand this` (as Phobos does) allows the indexing to be
performed directly on the tuple, without having to write
`.expand` first.
To handle dynamic vs. static indexing you can use a simple static
if:
static if (allSameTypes) {
auto opIndex(size_t i) {
// ...
}
} else {
alias expand this;
}
[1]
https://dlang.org/articles/ctarguments.html#type-seq-instantiation
More information about the Digitalmars-d-learn
mailing list