www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Default initialization of structs?

reply Gary Willoughby <dev nomad.so> writes:
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?
Jun 17 2016
next sibling parent reply Lodovico Giaretta <lodovico giaretart.net> writes:
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.
Jun 17 2016
parent reply Gary Willoughby <dev nomad.so> writes:
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.
Jun 17 2016
next sibling parent Namespace <rswhite4 gmail.com> writes:
The Factory-Pattern would be a good idea.
Jun 17 2016
prev sibling parent reply David Nadlinger <code klickverbot.at> writes:
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
Jun 17 2016
parent Kagamin <spam here.lot> writes:
On Friday, 17 June 2016 at 12:31:33 UTC, David Nadlinger wrote:
 Structs cannot have a default constructor; .init is required to 
 be a valid state (unless you  disable default construction).
Except for nested structs :) They have the default constructor and their .init is not a valid state: has null context pointer.
Jun 17 2016
prev sibling parent Johan Engelen <j j.nl> writes:
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
Jun 17 2016