Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 17, 2016 Default initialization of structs? | ||||
---|---|---|---|---|
| ||||
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? |
June 17, 2016 Re: Default initialization of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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.
|
June 17, 2016 Re: Default initialization of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lodovico Giaretta | 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.
|
June 17, 2016 Re: Default initialization of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | The Factory-Pattern would be a good idea. |
June 17, 2016 Re: Default initialization of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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
|
June 17, 2016 Re: Default initialization of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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
|
June 17, 2016 Re: Default initialization of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | 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.
|
Copyright © 1999-2021 by the D Language Foundation