Thread overview
std.typecons Typedef initializers?
Apr 25, 2022
Chris Katko
Apr 25, 2022
Mike Parker
Apr 25, 2022
Chris Katko
Apr 26, 2022
Stanislav Blinov
Apr 25, 2022
Paul Backus
April 25, 2022
struct pair
{
float x,y;
}

alias sPair = Typedef!pair; // pair of xy in screen space coordinates
alias vPair = Typedef!pair; // pair of xy in viewport space coordinates
//etc

void test()
{
pair v0 = pair(1f, 2f); // works fine, but what about the typedefs?

vPair v1 = vPair(1f, 2f); //nope

vPair v2 = Typedef!vPair(1f, 2f); //nope
}

How do you initialize a typedef'd struct?

April 25, 2022

On Monday, 25 April 2022 at 08:54:52 UTC, Chris Katko wrote:

>
struct pair
{
float x,y;
}

alias sPair = Typedef!pair; // pair of xy in screen space coordinates
alias vPair = Typedef!pair; // pair of xy in viewport space coordinates
//etc



How do you initialize a typedef'd struct?

``d
vPair v1 = vPair(pair(1f, 2f));

April 25, 2022

On Monday, 25 April 2022 at 08:54:52 UTC, Chris Katko wrote:

>
alias sPair = Typedef!pair; // pair of xy in screen space coordinates
alias vPair = Typedef!pair; // pair of xy in viewport space coordinates
//etc

This doesn't do what you think it does. Both sPair and vPair are the same type. If you want to create two distinct Typedefs from the same base type, you must use the optional cookie argument, as shown in the examples in the documentation:

alias MoneyEuros = Typedef!(float, float.init, "euros");
alias MoneyDollars = Typedef!(float, float.init, "dollars");

// The two Typedefs are _not_ the same type.
static assert(!is(MoneyEuros == MoneyDollars));
April 25, 2022

On Monday, 25 April 2022 at 12:53:14 UTC, Mike Parker wrote:

>

On Monday, 25 April 2022 at 08:54:52 UTC, Chris Katko wrote:

>
struct pair
{
float x,y;
}

alias sPair = Typedef!pair; // pair of xy in screen space coordinates
alias vPair = Typedef!pair; // pair of xy in viewport space coordinates
//etc



How do you initialize a typedef'd struct?

``d
vPair v1 = vPair(pair(1f, 2f));

So to use a typedef'd struct... I have to basically add the original type on top of the typedef'd type every time? Surely it's not this clunky?

I mean, why even use a typedef then. Why not use just pair, sPair, vPair, etc as separate types with identical members and cast as necessary? I'm not sure what the benefit typedef is adding here.

Thanks

April 26, 2022

On Monday, 25 April 2022 at 23:41:47 UTC, Chris Katko wrote:

>

So to use a typedef'd struct... I have to basically add the original type on top of the typedef'd type every time? Surely it's not this clunky?

I mean, why even use a typedef then. Why not use just pair, sPair, vPair, etc as separate types with identical members and cast as necessary? I'm not sure what the benefit typedef is adding here.

Thanks

It could just be an oversight in implementation and worth submitting an enhancement request on bugzilla.

Current implementation only defines a constructor that takes rvalue of original type, while what it ought to be doing is defining a variadic template constructor that would forward the arguments to underlying type's constructor.

To be fair, as far as your example code goes, it'd almost be easier to indeed simply duplicate the implementations, but have the compiler do it for you, e.g. like this:

enum Space
{
    unspecified,
    screen,
    viewport,
}

struct TPair(Space space) { float x, y; }

alias Pair = Pair!(Space.unspecified);
alias sPair = Pair!(Space.screen);
alias vPair = Pair!(Space.viewport);