Default struct constructor

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Feb 6 23:17:31 UTC 2019


On Wednesday, February 6, 2019 7:49:28 AM MST bitwise via Digitalmars-d 
wrote:
> On Thursday, 31 January 2019 at 13:57:41 UTC, Jonathan M Davis
>
> wrote:
> > It wouldn't necessarily be impossible to have it, but the
> > language was designed around default initialization and not
> > default construction, and adding default construction into the
> > mix would likely cause some confusing corner cases and
> > generally not work very well.
>
> If I can do this:
>
> struct A {
>      int n;
> }
>
> A a = void;
>
> Maybe I should be able to do this too:
>
> struct S {
>      this(void) { ... }   // <--- make more explicit by adding void
> }
>
> S s = S();

If that's what you want, then you can just use a static opCall. e.g.

struct S
{
    static S opCall()
    {
        ...
    }
}

auto s = S();

The only real restriction is that you can't declare any constructors if you
have any static opCall functions declared.

- Jonathan M Davis





More information about the Digitalmars-d mailing list