Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 4/25/22 21:32, Salih Dincer wrote: > So the problem is that the structure is not in the inter-module space Nested structs carry an additional pointer to their containing context. When they don't need the context, we define them with 'static': void foo() { static struct Bar { } } > If ``

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Salih Dincer via Digitalmars-d-learn
😀 So the problem is that the structure is not in the inter-module space On Monday, 25 April 2022 at 16:11:47 UTC, rassoc wrote: This works: ```d import std; void main() { struct Foo { string s; } Foo[] arr = ["abc", "def", "ghi"].map!Foo.array; arr.writeln; // => [Foo("abc"), F

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 4/25/22 19:37, Salih Dincer wrote: > a lot of errors 😀 Hm. I can't reproduce any of that. I did two things: 1) Added necessary import directives 2) Moved all expressions into the main() function I did not change anything else. The program below compiles and works with all these compilers:

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 26 April 2022 at 00:57:54 UTC, Ali Çehreli wrote: On 4/25/22 16:59, Salih Dincer wrote: > Because it cannot be used with other possibilities such as > ```chunks()``` and ```take()```. There must be something wrong. map is commonly used with chunks(), take(), etc. > Also it cannot

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 4/25/22 16:59, Salih Dincer wrote: > Because it cannot be used with other possibilities such as > ```chunks()``` and ```take()```. There must be something wrong. map is commonly used with chunks(), take(), etc. > Also it cannot be customized with > ```toString()```. Can you show examples

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 25 April 2022 at 16:11:47 UTC, rassoc wrote: ```d import std; void main() { struct Foo { string s; } Foo[] arr = ["abc", "def", "ghi"].map!Foo.array; arr.writeln; // => [Foo("abc"), Foo("def"), Foo("ghi")] } ``` Thank you... Very very nice and simple but not extensible

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread cc via Digitalmars-d-learn
On Monday, 25 April 2022 at 15:23:12 UTC, Ali Çehreli wrote: auto arr = iota(10).map!(i => Foo(i.text)).array; On Monday, 25 April 2022 at 16:11:47 UTC, rassoc wrote: Foo[] arr = ["abc", "def", "ghi"].map!Foo.array; Ahh that'll do it alright, thanks

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread rassoc via Digitalmars-d-learn
On 4/25/22 16:36, cc via Digitalmars-d-learn wrote: ```d struct Foo { string s; this(string s) { this.s = s; } } Foo foo = "a"; Foo[] foos = ["a"]; // Error: cannot implicitly convert expression `["a"]` of type `string[]` to `Foo[]` Foo[] foos = cast(Foo[]) ["a"]; // Error: e2ir: cann

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 4/25/22 07:36, cc wrote: > ```d > struct Foo { > string s; > this(string s) { this.s = s; } > } > Foo foo = "a"; I don't understand why that syntax exists. I always write it like this: auto foo = Foo("a"); > Foo[] foos = ["a"]; // Error: cannot implicitly convert expression > `["

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread cc via Digitalmars-d-learn
On Monday, 25 April 2022 at 15:13:51 UTC, Stanislav Blinov wrote: Make it explicit: ```d Foo[] foos = [Foo("a")]; ``` There's that too, but I still have to iterate manually. e.g.: ```d string[] ss = loadABunchOfStringsFromSomewhere(); //Foo[] foos = ss; //error Foo[] foos; foos.reserve(ss.len

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 25 April 2022 at 14:36:25 UTC, cc wrote: ```d struct Foo { string s; this(string s) { this.s = s; } } Foo foo = "a"; Foo[] foos = ["a"]; // Error: cannot implicitly convert expression `["a"]` of type `string[]` to `Foo[]` Foo[] foos = cast(Foo[]) ["a"]; // Error: e2ir:

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread cc via Digitalmars-d-learn
On Monday, 25 April 2022 at 15:00:13 UTC, Alain De Vos wrote: Not really an answer but this works, ``` void main(){ Foo foo = "a"; Foo[] foos; foos ~=foo; }% ``` Right, I can append individual elements, but can't assign or append a slice of a type that can be individually cast to the struct.

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
Not really an answer but this works, ``` struct Foo { string s; this(string s) { this.s = s; } } void main(){ Foo foo = "a"; Foo[] foos; foos ~=foo; }% ```