Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uniform initialization for type tuples too #18811

Open
dlangBugzillaToGithub opened this issue Apr 6, 2014 · 3 comments
Open

uniform initialization for type tuples too #18811

dlangBugzillaToGithub opened this issue Apr 6, 2014 · 3 comments

Comments

@dlangBugzillaToGithub
Copy link

monarchdodra reported this on 2014-04-06T12:14:28Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=12530

CC List

Description

Not sure if enhancement request or bug, but filing as EH.

I think these should work:

//----
    auto a = TypeTuple!(int, int, int)();� //(0, 0, 0)
    auto b = TypeTuple!(int, int, int)(1); //(1, 1, 1)
    //auto c = TypeTuple!(int, int, int)(1, 2);� //Should fail
    auto d = TypeTuple!(int, int, int)(1, 2, 3); //(1, 2, 3)
//----

Currently they all produce:
Error: function expected before (), not (int, int, int) of type (int, int, int)

In particular, I think this should work:
//----
    TypeTuple!(int, int, int) a = ...;
    TypeTuple!(int, int, int) b = TypeTuple!(int, int, int)(a);
//----
Though arguably, it's the exactly the same as the "d" case above.
@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 2014-04-06T12:32:55Z

(In reply to comment #0)

> I think these should work:
> 
>     auto a = TypeTuple!(int, int, int)();� //(0, 0, 0)
>     auto b = TypeTuple!(int, int, int)(1); //(1, 1, 1)
>     //auto c = TypeTuple!(int, int, int)(1, 2);� //Should fail
>     auto d = TypeTuple!(int, int, int)(1, 2, 3); //(1, 2, 3)

Please show one or more use cases.

@dlangBugzillaToGithub
Copy link
Author

monarchdodra commented on 2014-04-06T13:02:13Z

(In reply to comment #1)
> Please show one or more use cases.

I've had need of this, for example, to transform the types of variadic arguments, before passing them to another function. "reduce" or "uninitializedArray" come to mind:

//----
void main()
{
    uninitialized!(int[][])(1, 2);
}

alias ToSizeT(T) = size_t;
enum  IsSizeT(T) = is(S == size_t);

auto uninitialized(T, Args...)(Args args)
{
    //transform the "int" inference to "size_t"
    alias SizeTArgs = staticMap!(ToSizeT, Args);

    //Call with the same arguments, but cast to size_t...
    //if the cast is implicitly safe
    return impl!T(SizeTArgs(args));
}

auto impl(T, Args...)(Args args) //Avoid bloat by requesting size_t args.
{
    static assert(allSatisfy!(IsSizeT, Args));
}
//----

The same design can be used to transform/forward args, say to unqualify all of them at once:
//----
void myFun(Args(Args args))
{
    alias UArgs = staticMap!(Unqual, Args);
    static if (!is(UArgs == ARgs))
        myFun(UArgs(args));
    else
    {
        //actual implementation here
    }
}

Or, in the case of reduce, to build the different seeds from front:
auto result = tuple(SeedTypes(r.front));

@dlangBugzillaToGithub
Copy link
Author

nick (@ntrel) commented on 2024-08-23T16:47:24Z

>    auto b = TypeTuple!(int, int, int)(1); //(1, 1, 1)
...
>    TypeTuple!(int, int, int) a = ...;
>    TypeTuple!(int, int, int) b = TypeTuple!(int, int, int)(a);

Not uniform initialization, but you can do:

import std.meta: AliasSeq;

AliasSeq!(int, int, int) vs = 1;
assert(vs == AliasSeq!(1, 1, 1));

AliasSeq!(int, int, int) xs = vs;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant