digitalmars.D.learn - Default initialization of structs?
- Gary Willoughby (15/15) Jun 17 2016 I have a struct where I need to perform default initialization of
- Lodovico Giaretta (11/26) Jun 17 2016 struct Foo(T)
- Gary Willoughby (14/24) Jun 17 2016 Thanks, I forgot to mention I'm also doing lots of other stuff in
- Namespace (1/1) Jun 17 2016 The Factory-Pattern would be a good idea.
- David Nadlinger (8/21) Jun 17 2016 Structs cannot have a default constructor; .init is required to
- Kagamin (3/5) Jun 17 2016 Except for nested structs :) They have the default constructor
- Johan Engelen (5/8) Jun 17 2016 This is a fairly recent change (2.068->2.069 or 2.070), so if you
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
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
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
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
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
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