Named constructors

12345swordy alexanderheistermann at gmail.com
Wed Jan 9 18:24:28 UTC 2019


On Wednesday, 9 January 2019 at 18:19:35 UTC, Daniel Kozak wrote:
> On Wed, Jan 9, 2019 at 5:50 PM Neia Neutuladh via Digitalmars-d 
> < digitalmars-d at puremagic.com> wrote:
>
>>[...]
>
> import std.stdio;
> import std.math;
>
> void main()
> {
>     auto sa = S.constructorA(3,4);
>     auto sb = S.constructorB(3,4);
>     writeln(sa);
>     writeln(sb);
> }
>
>
> struct S {
> public:
>    this(string name: "constructorA")(real x, real y)
>    {
>        this.x = x;
>        this.y = y;
>    }
>
>    this(string name: "constructorB")(real x, real z)
>    {
>        this.x = x;
>        this.z = z;
>    }
>
>    static constructorA(Args...)(Args args)
>    {
>        S s;
>        s.__ctor!"constructorA"(args);
>        return s;
>    }
>
>    static constructorB(Args...)(Args args)
>    {
>        S s;
>        s.__ctor!"constructorB"(args);
>        return s;
>    }
>    const real x, y, z;
> }
That solution has a compile time cost.


More information about the Digitalmars-d mailing list