initialization of structs

2011-09-26 Thread Christian Köstlin
Hi, I have the problem, that I want to always construct a struct with a parameter. To make this more comfortable (e.g. provide a default parameter I have a factory function to create this struct). struct S { int i; this(int i_) { i = i_; } } S createS(int i=5) { return S(i); } My ques

Re: initialization of structs

2011-09-26 Thread Andrej Mitrovic
You can hide the constructor from external code by using "private this()". And you can disable the default constructor via @disable this(); You may also wish to make your fields private as well. You don't necessarily have to make createS() a free function, you can make it a static function inside

Re: initialization of structs

2011-09-26 Thread Timon Gehr
On 09/26/2011 10:55 PM, Christian Köstlin wrote: Hi, I have the problem, that I want to always construct a struct with a parameter. To make this more comfortable (e.g. provide a default parameter I have a factory function to create this struct). struct S { int i; this(int i_) { i = i_; } } S c

Re: initialization of structs

2011-09-26 Thread Andrej Mitrovic
On 9/26/11, Andrej Mitrovic wrote: > create() will not occupy any space in any of the S instances. Sorry for that stupid comment. struct methods don't occupy space in struct instances regardless if they're static or not.

Re: initialization of structs

2011-09-26 Thread Christian Köstlin
On 9/26/11 23:18 , Andrej Mitrovic wrote: You can hide the constructor from external code by using "private this()". And you can disable the default constructor via @disable this(); You may also wish to make your fields private as well. You don't necessarily have to make createS() a free functio

Re: initialization of structs

2011-09-27 Thread Christian Köstlin
On 09/26/2011 11:15 PM, Timon Gehr wrote: On 09/26/2011 10:55 PM, Christian Köstlin wrote: Hi, I have the problem, that I want to always construct a struct with a parameter. To make this more comfortable (e.g. provide a default parameter I have a factory function to create this struct). struct

static initialization of structs

2012-12-01 Thread Dan
In the thread on compiling DSSS and build tools someone mentioned a format for initializing environment data of a struct: Environment env = { tests: true, verbose: true, importDirs: ["../deimos"] } A followup comment says "isn't that syntax intended to be deprecated?"

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!(stri

"Lazy" initialization of structs

2017-06-01 Thread Daniel Tan Fook Hao via Digitalmars-d-learn
Somehow this code works for me: ```D auto error (int status, string description){ struct Error { int status; string description; } Error err = { status, description }; return err.serializeToJson; } ``` which is supposed to be the same as ```D

Re: static initialization of structs

2012-12-01 Thread Rob T
On Saturday, 1 December 2012 at 18:36:35 UTC, Dan wrote: In the thread on compiling DSSS and build tools someone mentioned a format for initializing environment data of a struct: Environment env = { tests: true, verbose: true, importDirs: ["../deimos"] } A followup co

Re: static initialization of structs

2012-12-01 Thread Jonathan M Davis
On Saturday, December 01, 2012 19:36:34 Dan wrote: > In the thread on compiling DSSS and build tools someone mentioned > a format for initializing environment data of a struct: > >Environment env = { > tests: true, > verbose: true, > importDirs: ["../deimos"] >} >

Re: static initialization of structs

2012-12-01 Thread Dan
On Sunday, 2 December 2012 at 01:50:08 UTC, Jonathan M Davis wrote: On Saturday, December 01, 2012 19:36:34 Dan wrote: That syntax is from C. There was definitely a push to deprecate it, and personally I definitely think that it should go, but I don't recall that it was definitively decided t

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

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 con

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 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),

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; th

Re: Default initialization of structs?

2016-06-17 Thread Kagamin via Digitalmars-d-learn
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

Re: "Lazy" initialization of structs

2017-06-01 Thread Era Scarecrow via Digitalmars-d-learn
On Thursday, 1 June 2017 at 12:04:05 UTC, Daniel Tan Fook Hao wrote: If I'm reading this right, in the former, the struct is created when the function is called in run-time, and the type is then inferred after that? I don't really understand the behavior behind this. The only difference betw

Re: "Lazy" initialization of structs

2017-06-01 Thread Nicholas Wilson via Digitalmars-d-learn
On Thursday, 1 June 2017 at 12:04:05 UTC, Daniel Tan Fook Hao wrote: Somehow this code works for me: ```D auto error (int status, string description){ struct Error { int status; string description; } Error err = { status, description }; return

Re: "Lazy" initialization of structs

2017-06-01 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 1 June 2017 at 12:04:05 UTC, Daniel Tan Fook Hao wrote: Somehow this code works for me: ```D auto error (int status, string description){ struct Error { int status; string description; } Error err = { status, description }; return

Static Initialization of Structs syntax

2022-06-22 Thread Antonio via Digitalmars-d-learn
I'm so sorry, I know that the above example doesn't work: ```d main(){ Struct PersonDTO { string name; string surname; } void create(PersonDTO personData){ // ... } create( {name: "Peter", surname: "Ustinov"} ); } ``` -Is it there any alternative to initialize "inline" d

Re: Static Initialization of Structs syntax

2022-06-22 Thread Antonio via Digitalmars-d-learn
On Wednesday, 22 June 2022 at 09:41:55 UTC, Antonio wrote: I'm so sorry, I know that the above example doesn't work: ```d main(){ Struct PersonDTO { string name; string surname; } void create(PersonDTO personData){ // ... } create( {name: "Peter", surname: "Ustinov"} ); }

Re: Static Initialization of Structs syntax

2022-06-22 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 22 June 2022 at 11:19:59 UTC, Antonio wrote: I see now: DIP 1033 will solve this (i.e., using named arguments in struct constructor... similar to how dart/flutter works) That would be DIP 1030: https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1030.md Max Haughton