D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4582 - distinct field names constraint for std.typecons.Tuple
Summary: distinct field names constraint for std.typecons.Tuple
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: bootcamp, patch
Depends on:
Blocks:
 
Reported: 2010-08-04 17:21 UTC by bearophile_hugs
Modified: 2017-10-16 09:58 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-08-04 17:21:25 UTC
It's better to put guards as template constraints at the std.typecons.Tuple, so erroneous Tuple template instantiations fail at the instantiation point, giving a more useful error message in the user code instead giving an error inside the std.typecons module (this is one of the main purposes of template constraints).

This code guards against duplicated field names:


private template Iota(int stop) { // this is useful in general
    static if (stop <= 0)
        alias TypeTuple!() Iota;
    else
        alias TypeTuple!(Iota!(stop-1), stop-1) Iota;
}

private bool distinctFieldNames(T...)() {
    enum int tlen = T.length; // can't move this below, probably DMD bug
    foreach (i1; Iota!(tlen))
        static if (is(typeof(T[i1]) : string))
            foreach (i2; Iota!(tlen))
                static if (i1 != i2 && is(typeof(T[i2]) : string))
                    if (T[i1] == T[i2])
                        return false;
    return true;
}

// ...............

struct Tuple(T...) if (distinctFieldNames!(T)())
{
Comment 1 Andrei Alexandrescu 2010-10-07 08:02:31 UTC
Excellent.
Comment 2 bearophile_hugs 2010-10-07 13:25:43 UTC
Thank you. But I think some tests/benchmarks may be done to make sure this extra test doesn't slow down too much the compilation of programs that define many tuple types.
Comment 3 github-bugzilla 2017-10-12 13:20:35 UTC
Commits pushed to master at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/4e8fa84ba227b2ff4662b2e2fcdeca57074bc2fd
Fix Issue 4582 - distinct field names constraint for std.typecons.Tuple

https://github.com/dlang/phobos/commit/f24da9d895b6be51c0b1d196eb0c9d0e67f81dc1
Merge pull request #5725 from RazvanN7/Issue_4582

Fix Issue 4582 - distinct field names constraint for std.typecons.Tuple
merged-on-behalf-of: MetaLang <MetaLang@users.noreply.github.com>
Comment 4 github-bugzilla 2017-10-16 09:58:08 UTC
Commits pushed to stable at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/4e8fa84ba227b2ff4662b2e2fcdeca57074bc2fd
Fix Issue 4582 - distinct field names constraint for std.typecons.Tuple

https://github.com/dlang/phobos/commit/f24da9d895b6be51c0b1d196eb0c9d0e67f81dc1
Merge pull request #5725 from RazvanN7/Issue_4582