Re: Default initialization of structs?

2016-06-17 Thread David Nadlinger via Digitalmars-d-learn

On Friday, 17 June 2016 at 11:10:12 UTC, Gary Willoughby wrote:
Thanks, I forgot to mention I'm also doing lots of other stuff 
in the constructor to private fields too.


struct Foo(T)
{
private int _bar;
private void* _baz;

this(int bar = 8)
{
this._bar = bar;
this._baz = malloc(this._bar);
}
}

So I have to at least run a constructor.


Structs cannot have a default constructor; .init is required to 
be a valid state (unless you @disable default construction). This 
is a deliberate language restriction, although you can argue 
about its value.


What you can do as a workaround is to provide a public static 
factory method while disabling default construction.


 — David


Re: Default initialization of structs?

2016-06-17 Thread Johan Engelen via Digitalmars-d-learn

On Friday, 17 June 2016 at 10:50:55 UTC, Gary Willoughby wrote:
I have a struct where I need to perform default initialization 
of some members but the compiler doesn't allow to define a 
default constructor which allow optional arguments.


This is a fairly recent change (2.068->2.069 or 2.070), so if you 
browse the release notes you may be able to figure out exactly 
why this is not allowed.


-Johan


Re: Default initialization of structs?

2016-06-17 Thread Namespace via Digitalmars-d-learn

The Factory-Pattern would be a good idea.


Re: Default initialization of structs?

2016-06-17 Thread Gary Willoughby via Digitalmars-d-learn

On Friday, 17 June 2016 at 10:53:40 UTC, Lodovico Giaretta wrote:

struct Foo(T)
{
private int _bar = 1;

this(int bar)
{
this._bar = bar;
}
}

auto foo = Foo!(string)();

This should do the trick.


Thanks, I forgot to mention I'm also doing lots of other stuff in 
the constructor to private fields too.


struct Foo(T)
{
private int _bar;
private void* _baz;

this(int bar = 8)
{
this._bar = bar;
this._baz = malloc(this._bar);
}
}

So I have to at least run a constructor.



Re: Default initialization of structs?

2016-06-17 Thread Lodovico Giaretta via Digitalmars-d-learn

On Friday, 17 June 2016 at 10:50:55 UTC, Gary Willoughby wrote:
I have a struct where I need to perform default initialization 
of some members but the compiler doesn't allow to define a 
default constructor which allow optional arguments.


struct Foo(T)
{
private int _bar;

this(int bar = 1)
{
this._bar = bar;
}
}

auto foo = Foo!(string) // error

Are there any patterns or idioms I could use to get the desired 
result? Or is it just the case if I use a constructor I have to 
pass values to it?


struct Foo(T)
{
private int _bar = 1;

this(int bar)
{
this._bar = bar;
}
}

auto foo = Foo!(string)();

This should do the trick.


Default initialization of structs?

2016-06-17 Thread Gary Willoughby via Digitalmars-d-learn
I have a struct where I need to perform default initialization of 
some members but the compiler doesn't allow to define a default 
constructor which allow optional arguments.


struct Foo(T)
{
private int _bar;

this(int bar = 1)
{
this._bar = bar;
}
}

auto foo = Foo!(string) // error

Are there any patterns or idioms I could use to get the desired 
result? Or is it just the case if I use a constructor I have to 
pass values to it?