digitalmars.D.learn - std.typecons Typedef initializers?
- Chris Katko (19/19) Apr 25 2022 ````D
- Mike Parker (4/15) Apr 25 2022 ``d
- Chris Katko (9/28) Apr 25 2022 So to use a typedef'd struct... I have to basically add the
- Stanislav Blinov (22/30) Apr 25 2022 It could just be an oversight in implementation and worth
- Paul Backus (13/20) Apr 25 2022 This doesn't do what you think it does. Both `sPair` and `vPair`
````D 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?
Apr 25 2022
On Monday, 25 April 2022 at 08:54:52 UTC, Chris Katko wrote:````D 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)); ```
Apr 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: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````D 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)); ```
Apr 25 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. ThanksIt 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: ```d 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); ```
Apr 25 2022
On Monday, 25 April 2022 at 08:54:52 UTC, Chris Katko wrote:````D 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 `Typedef`s from the same base type, you must use the optional `cookie` argument, as shown in [the examples in the documentation:][1] ```d 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)); ``` [1]: https://phobos.dpldocs.info/std.typecons.Typedef.html#examples
Apr 25 2022