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

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

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

Re: Get UDAs of self's declaration as a member?

2022-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 4/25/22 14:32, cc wrote: > Hard to word this question right, but is it possible to get the UDAs > assigned to a class/structure's member variable declaration, within that > variable's definition? e.g. That sounds backwards to me too. :) Policy-based design can work here: import std.stdio;

Re: How to use destroy and free.

2022-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 4/25/22 16:02, frame wrote: > On Monday, 25 April 2022 at 02:07:50 UTC, Ali Çehreli wrote: >> > import core.memory: GC; >> GC.free(GC.addrOf(cast(void *)(i.ptr))); >> That is wrong because you did not allocate that address yourself. > > Hmm? The GC did allocate here(?) Yes. I still

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: std.typecons Typedef initializers?

2022-04-25 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 25 April 2022 at 23:41:47 UTC, Chris Katko wrote: So to use a typedef'd struct... I have to basically add the original type on top of the typedef'd type every time? Surely it's not this clunky? I mean, why even use a typedef then. Why not use just pair, sPair, vPair, etc as

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

Re: std.typecons Typedef initializers?

2022-04-25 Thread Chris Katko via Digitalmars-d-learn
On Monday, 25 April 2022 at 12:53:14 UTC, Mike Parker wrote: On Monday, 25 April 2022 at 08:54:52 UTC, Chris Katko wrote: D struct pair { float x,y; } alias sPair = Typedef!pair; // pair of xy in screen space coordinates alias vPair = Typedef!pair; // pair of xy in viewport space

Re: How to use destroy and free.

2022-04-25 Thread frame via Digitalmars-d-learn
On Monday, 25 April 2022 at 02:07:50 UTC, Ali Çehreli wrote: > import core.memory: GC; GC.free(GC.addrOf(cast(void *)(i.ptr))); That is wrong because you did not allocate that address yourself. Hmm? The GC did allocate here(?) On 4/24/22 17:26, Salih Dincer wrote: >

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

Get UDAs of self's declaration as a member?

2022-04-25 Thread cc via Digitalmars-d-learn
Hard to word this question right, but is it possible to get the UDAs assigned to a class/structure's member variable declaration, within that variable's definition? e.g. ```d import std.stdio; import std.traits; enum SPECIAL; struct Foo { void foo() { static if

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:

Re: Linked list, printing looks destructive.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
Indeed code below works, ``` import std.stdio: write,writeln; class Node { int data; Node next; } class List { Node node=null; this(int[] AR){foreach(i ; AR)pushfront(i);} void pushfront(int data) { Node newnode=new Node();

Re: Linked list, printing looks destructive.

2022-04-25 Thread cc via Digitalmars-d-learn
On Monday, 25 April 2022 at 01:40:01 UTC, Alain De Vod wrote: Following program is a single linked list. We expect as output 1 2 3 1 2 3 But the output is only 1 2 3 ``` If you don't need List to be treated as a true range, but just want to iterate, a simple way to do this is with opApply:

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;

Re: Linked list, printing looks destructive.

2022-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 4/25/22 03:48, Salih Dincer wrote: > It is also possible with the copy constructor of a struct. I don't know > how to do with class... Classes don't have language provided construction because nobody needs it and in fact they have to protect themselves when a language provides it. (See,

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:

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; }% ```

Assigning to array of structs with custom constructor

2022-04-25 Thread cc via Digitalmars-d-learn
```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: cannot cast `"a"` of type `string` to type `Foo`

Re: How to use destroy and free.

2022-04-25 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Apr 25, 2022 at 01:28:01PM +, Alain De Vos via Digitalmars-d-learn wrote: > Could thc or hboehm provide solutions ? In general, GC (of any kind) does not (and cannot) guarantee the order objects will be collected in. So in the dtor, you cannot assume that any objects you depend on

Re: How to use destroy and free.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
Could thc or hboehm provide solutions ?

Re: How to use destroy and free.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
GC-allocated objects are run (when they are run). If you need deterministic destruction e.g. for resource management, do not use GC. Descend destroy and free functions should return something. "destroy" should return if the destructor was called successfully. "free" should return the exact

Re: How to implement private constructor

2022-04-25 Thread Vinod K Chandran via Digitalmars-d-learn
On Monday, 25 April 2022 at 07:19:31 UTC, bauss wrote: Yes and in addition to Ali's message then remember it's private for the module only. Oops typo. What I meant is that private is module level, so it's __not__ private in the module, but it is for other modules. Thanks for the reply.

Re: std.typecons Typedef initializers?

2022-04-25 Thread Paul Backus via Digitalmars-d-learn
On Monday, 25 April 2022 at 08:54:52 UTC, Chris Katko wrote: D alias sPair = Typedef!pair; // pair of xy in screen space coordinates alias vPair = Typedef!pair; // pair of xy in viewport space coordinates //etc This doesn't do what you think it does. Both `sPair` and `vPair` are

Re: How to implement private constructor

2022-04-25 Thread Vinod K Chandran via Digitalmars-d-learn
On Monday, 25 April 2022 at 02:22:42 UTC, Ali Çehreli wrote: Looks good to me. There are other ways as well: Thanks a lot. All I wanted to implement more than ctor with different parameters and avoid code duplication.

Re: std.typecons Typedef initializers?

2022-04-25 Thread Mike Parker via Digitalmars-d-learn
On Monday, 25 April 2022 at 08:54:52 UTC, Chris Katko wrote: D struct pair { float x,y; } alias sPair = Typedef!pair; // pair of xy in screen space coordinates alias vPair = Typedef!pair; // pair of xy in viewport space coordinates //etc How do you initialize a typedef'd struct? ``d

Re: How to use destroy and free.

2022-04-25 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 25 April 2022 at 10:13:43 UTC, Alain De Vos wrote: Ali, thanks for the answer but i rephrase my question. How to destroy,free , for garbage-collection-cycle in the destructor of this code : // But How to force destroy and free , GC-cycle for heap object i ? Short answer: use

Re: How to use destroy and free.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
Note, heap object i is not an instance of the class C

Re: Linked list, printing looks destructive.

2022-04-25 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 25 April 2022 at 09:38:05 UTC, Alain De Vos wrote: This program works ok, (but List is no Range) It is also possible with the copy constructor of a struct. I don't know how to do with class... ```d struct Node { int element; Node * next; }

Re: How to use destroy and free.

2022-04-25 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 25 April 2022 at 10:13:43 UTC, Alain De Vos wrote: destructor of this code : ```d ~this(){ writeln("Free heap"); import object: destroy; import core.memory: GC; i=null; // But How to force destroy and free , GC-cycle for heap object i ? }; ``` If

Re: How to use destroy and free.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
Ali, thanks for the answer but i rephrase my question. How to destroy,free , for garbage-collection-cycle in the destructor of this code : ``` import std.stdio: writeln; class C{ int[] i=null; this(){ writeln("Allocate heap"); i=new int[1];

Re: Linked list, printing looks destructive.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
This program works ok, (but List is no Range) ``` class Node { int data; Node next; } class List { Node node=null; this(int[] AR){foreach(i ; AR)pushfront(i);} bool empty() const {return !node;} void popFront() {node=node.next;} float

std.typecons Typedef initializers?

2022-04-25 Thread Chris Katko via Digitalmars-d-learn
D struct pair { float x,y; } alias sPair = Typedef!pair; // pair of xy in screen space coordinates alias vPair = Typedef!pair; // pair of xy in viewport space coordinates //etc void test() { pair v0 = pair(1f, 2f); // works fine, but what about the typedefs? vPair v1 = vPair(1f, 2f);

Re: Linked list, printing looks destructive.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
On Monday, 25 April 2022 at 05:17:28 UTC, Salih Dincer wrote: On Monday, 25 April 2022 at 02:19:46 UTC, Ali Çehreli wrote: This type violates a fundamental rule: Containers and ranges are separate concepts. Your List is a container, not a range. I changed your code by moving the range

Re: How to implement private constructor

2022-04-25 Thread bauss via Digitalmars-d-learn
On Monday, 25 April 2022 at 07:18:44 UTC, bauss wrote: On Monday, 25 April 2022 at 00:18:03 UTC, Vinod K Chandran wrote: Hi all, Please take a look at this code. Is this the right way to use private constructors ? ```d class Foo { int p1 ; string p2 ; bool p3 ; private

Re: How to implement private constructor

2022-04-25 Thread bauss via Digitalmars-d-learn
On Monday, 25 April 2022 at 00:18:03 UTC, Vinod K Chandran wrote: Hi all, Please take a look at this code. Is this the right way to use private constructors ? ```d class Foo { int p1 ; string p2 ; bool p3 ; private this(int a, string b, bool c) { this.p1 = a