Re: Linked list, printing looks destructive.

2022-04-24 Thread Salih Dincer via Digitalmars-d-learn
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 functions to a Range [...] Dear Ali, I implemented a linkedlist

Re: How to implement private constructor

2022-04-24 Thread Ali Çehreli via Digitalmars-d-learn
On 4/24/22 17:18, Vinod K Chandran wrote: > private this(int a, string b, bool c) { Looks good to me. There are other ways as well: class Foo { private: // ... public: // ... } Or: class Foo { private { // ... } // ... } Ali

Re: Linked list, printing looks destructive.

2022-04-24 Thread Ali Çehreli via Digitalmars-d-learn
On 4/24/22 18:40, Alain De Vod wrote: > I think this has something to do with popFront 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 functions to a Range struct that is created

Re: How to use destroy and free.

2022-04-24 Thread Ali Çehreli via Digitalmars-d-learn
On 4/24/22 17:26, Salih Dincer wrote: > first destroy() then free()... Makes sense only if we allocated the memory. > import object: doDestroy = destroy; I like adding 'do' to verbs that can be confused with nouns. For example, because 'copy' is both a noun and a verb, I think it helps when

Re: How to use destroy and free.

2022-04-24 Thread Ali Çehreli via Digitalmars-d-learn
On 4/24/22 14:00, Alain De Vod wrote: > Is this a correct program to explicit call destroy & free ? destroy() is called with the object that you want its destructor to be executed on. This is very rare in D because when the destructor has to be called, one relies on the lifetime of a struct

Linked list, printing looks destructive.

2022-04-24 Thread Alain De Vod via Digitalmars-d-learn
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 I think this has something to do with popFront How to fix it using "class List" ? ``` import std.stdio: write,writeln; import std.range: empty,popFront,front; struct Node { int

Re: How to use destroy and free.

2022-04-24 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 24 April 2022 at 21:00:50 UTC, Alain De Vod wrote: Is this a correct program to explicit call destroy & free ? ``` void main(){ int[] i=new int[1]; import object: destroy; destroy(i); import core.memory: GC; GC.free(GC.addrOf(cast(void *)(i.ptr))); } ```

How to implement private constructor

2022-04-24 Thread Vinod K Chandran via Digitalmars-d-learn
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 this.p2 = b this.p3 = c } this(int a) {

How to use destroy and free.

2022-04-24 Thread Alain De Vod via Digitalmars-d-learn
Is this a correct program to explicit call destroy & free ? ``` void main(){ int[] i=new int[1]; import object: destroy; destroy(i); import core.memory: GC; GC.free(GC.addrOf(cast(void *)(i.ptr))); } ```

Re: Create a wrapper around larger c-libraries

2022-04-24 Thread user1234 via Digitalmars-d-learn
On Sunday, 24 April 2022 at 15:13:15 UTC, Alain De Vos wrote: I'm currenlty experimenting about binding to C. I have : C-library: mylib.h: ``` void libprintme(char *s); ``` mylib.c: ``` #include #include "mylib.h" void libprintme(char *s){printf("%s",s);} ``` [...] Can this procedure be

Re: Create a wrapper around larger c-libraries

2022-04-24 Thread Alain De Vos via Digitalmars-d-learn
``` void main(){dprintme("Hello\n");} } ```

Create a wrapper around larger c-libraries

2022-04-24 Thread Alain De Vos via Digitalmars-d-learn
I'm currenlty experimenting about binding to C. I have : C-library: mylib.h: ``` void libprintme(char *s); ``` mylib.c: ``` #include #include "mylib.h" void libprintme(char *s){printf("%s",s);} ``` main.d: ``` extern(C) @nogc nothrow { void libprintme(char *s); alias pprintme=void

Re: Is @live attribute working yet?

2022-04-24 Thread Paolo Invernizzi via Digitalmars-d-learn
On Sunday, 24 April 2022 at 12:21:29 UTC, elfstone wrote: On Sunday, 24 April 2022 at 11:36:29 UTC, Paolo Invernizzi wrote: On Sunday, 24 April 2022 at 09:31:40 UTC, elfstone wrote: [...] You need to use the flag `-preview=dip1021` test.d(8,30): Error: variable `test.test.p` assigning

Re: Is @live attribute working yet?

2022-04-24 Thread elfstone via Digitalmars-d-learn
On Sunday, 24 April 2022 at 11:36:29 UTC, Paolo Invernizzi wrote: On Sunday, 24 April 2022 at 09:31:40 UTC, elfstone wrote: [...] You need to use the flag `-preview=dip1021` test.d(8,30): Error: variable `test.test.p` assigning to Owner without disposing of owned value Weird, it's

Re: Is @live attribute working yet?

2022-04-24 Thread elfstone via Digitalmars-d-learn
On Sunday, 24 April 2022 at 11:07:43 UTC, Tejas wrote: On Sunday, 24 April 2022 at 09:31:40 UTC, elfstone wrote: Dub(DMD 2.099.1) builds and runs the following code without a warning. import std.stdio; import core.stdc.stdlib; @live void test() { int* p =

Re: Is @live attribute working yet?

2022-04-24 Thread Paolo Invernizzi via Digitalmars-d-learn
On Sunday, 24 April 2022 at 09:31:40 UTC, elfstone wrote: Dub(DMD 2.099.1) builds and runs the following code without a warning. import std.stdio; import core.stdc.stdlib; @live void test() { int* p = cast(int*) malloc(32); p = cast(int*) malloc(32);

Re: Is @live attribute working yet?

2022-04-24 Thread Tejas via Digitalmars-d-learn
On Sunday, 24 April 2022 at 09:31:40 UTC, elfstone wrote: Dub(DMD 2.099.1) builds and runs the following code without a warning. import std.stdio; import core.stdc.stdlib; @live void test() { int* p = cast(int*) malloc(32); p = cast(int*) malloc(32);

Is @live attribute working yet?

2022-04-24 Thread elfstone via Digitalmars-d-learn
Dub(DMD 2.099.1) builds and runs the following code without a warning. import std.stdio; import core.stdc.stdlib; @live void test() { int* p = cast(int*) malloc(32); p = cast(int*) malloc(32); free(p); } void main() { test();