Re: I do not understand copy constructors

2021-08-13 Thread Paul Backus via Digitalmars-d-learn
On Friday, 13 August 2021 at 21:34:29 UTC, Steven Schveighoffer wrote: But for constructors it's not the same. Essentially because constructors have different rules for what they can do with their inputs (the inout `this` parameter can be assigned to for the member's first assignment). What I

Re: I do not understand copy constructors

2021-08-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/13/21 4:58 PM, Paul Backus wrote: On Friday, 13 August 2021 at 15:26:15 UTC, Steven Schveighoffer wrote: The issue is that you can't convert const (or immutable or mutable) to inout implicitly, and the member variable is inout inside an inout constructor. Therefore, there's no viable copy

Re: I do not understand copy constructors

2021-08-13 Thread Paul Backus via Digitalmars-d-learn
On Friday, 13 August 2021 at 15:26:15 UTC, Steven Schveighoffer wrote: The issue is that you can't convert const (or immutable or mutable) to inout implicitly, and the member variable is inout inside an inout constructor. Therefore, there's no viable copy constructor to call for the member, and

Re: I do not understand copy constructors

2021-08-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/12/21 12:12 PM, Paul Backus wrote: The reason for this is a bit subtle. Normally, `inout` can convert to `const`, so you might expect that the `const` copy constructor could be used to construct a copy of an `inout` object. However, copy constructors take their arguments by `ref`, and impl

Re: I do not understand copy constructors

2021-08-12 Thread Ali Çehreli via Digitalmars-d-learn
On 8/12/21 4:32 AM, Paul Backus wrote: > Qualifying the ctor as `inout` works fine I can see how a DConf Online presention is shaping up in your head. ;) http://dconf.org/2021/online/index.html We need a collective understanding of effective use of such fundamental concepts. Ali

Re: I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
On Thursday, 12 August 2021 at 16:12:39 UTC, Paul Backus wrote: On Thursday, 12 August 2021 at 15:39:40 UTC, Learner wrote: [...] You have forgotten to add a member variable of type `A` to your `B` struct. If you add one, you will see the following error message: [...] "implicit conversi

Re: I do not understand copy constructors

2021-08-12 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 12 August 2021 at 15:39:40 UTC, Learner wrote: It is not clear to me why the inout generated copy constructor of the B structure is not able to copy the A structure. struct A { int[] data; this(ref return scope A rhs){ /* body */ }

Re: I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
On Thursday, 12 August 2021 at 15:50:05 UTC, Tejas wrote: On Thursday, 12 August 2021 at 15:39:40 UTC, Learner wrote: On Thursday, 12 August 2021 at 14:57:16 UTC, Steven Schveighoffer wrote: [...] It is not clear to me why the inout generated copy constructor of the B structure is not able t

Re: I do not understand copy constructors

2021-08-12 Thread Tejas via Digitalmars-d-learn
On Thursday, 12 August 2021 at 15:39:40 UTC, Learner wrote: On Thursday, 12 August 2021 at 14:57:16 UTC, Steven Schveighoffer wrote: [...] It is not clear to me why the inout generated copy constructor of the B structure is not able to copy the A structure. [...] Why will copy constructor

Re: I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
On Thursday, 12 August 2021 at 14:57:16 UTC, Steven Schveighoffer wrote: On 8/12/21 10:08 AM, Learner wrote: On Thursday, 12 August 2021 at 13:56:17 UTC, Paul Backus wrote: On Thursday, 12 August 2021 at 12:10:49 UTC, Learner wrote: That worked fine, but the codebase is @safe: ```d cast from

Re: I do not understand copy constructors

2021-08-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/12/21 10:08 AM, Learner wrote: On Thursday, 12 August 2021 at 13:56:17 UTC, Paul Backus wrote: On Thursday, 12 August 2021 at 12:10:49 UTC, Learner wrote: That worked fine, but the codebase is @safe: ```d cast from `int[]` to `inout(int[])` not allowed in safe code ``` So copy construct

Re: I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
On Thursday, 12 August 2021 at 13:56:17 UTC, Paul Backus wrote: On Thursday, 12 August 2021 at 12:10:49 UTC, Learner wrote: That worked fine, but the codebase is @safe: ```d cast from `int[]` to `inout(int[])` not allowed in safe code ``` So copy constructors force me to introduce trusted met

Re: I do not understand copy constructors

2021-08-12 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 12 August 2021 at 12:10:49 UTC, Learner wrote: That worked fine, but the codebase is @safe: ```d cast from `int[]` to `inout(int[])` not allowed in safe code ``` So copy constructors force me to introduce trusted methods, while that was not necessary with postblits? A postblit

Re: I do not understand copy constructors

2021-08-12 Thread Tejas via Digitalmars-d-learn
On Thursday, 12 August 2021 at 12:28:32 UTC, Learner wrote: On Thursday, 12 August 2021 at 12:22:22 UTC, Tejas wrote: On Thursday, 12 August 2021 at 12:19:56 UTC, Tejas wrote: [...] Works with ```@safe``` as well Paul was just trying to make that other answer work, you don't have to make co

Re: I do not understand copy constructors

2021-08-12 Thread Tejas via Digitalmars-d-learn
On Thursday, 12 August 2021 at 12:22:22 UTC, Tejas wrote: On Thursday, 12 August 2021 at 12:19:56 UTC, Tejas wrote: [...] Works with ```@safe``` as well Paul was just trying to make that other answer work, you don't have to make copy constructors ```@trusted``` Ignore this, it doesn't wor

Re: I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
On Thursday, 12 August 2021 at 12:22:22 UTC, Tejas wrote: On Thursday, 12 August 2021 at 12:19:56 UTC, Tejas wrote: [...] Works with ```@safe``` as well Paul was just trying to make that other answer work, you don't have to make copy constructors ```@trusted``` Operations are needed on `ot

Re: I do not understand copy constructors

2021-08-12 Thread Tejas via Digitalmars-d-learn
On Thursday, 12 August 2021 at 12:19:56 UTC, Tejas wrote: On Thursday, 12 August 2021 at 11:54:22 UTC, Learner wrote: [...] Just add ```inout``` inside ```this(ref inout/*notice the inout*/ Foo other) inout/*notice the inout*/``` Example code: ```d struct Foo { this(ref inout Foo ot

Re: I do not understand copy constructors

2021-08-12 Thread Tejas via Digitalmars-d-learn
On Thursday, 12 August 2021 at 11:54:22 UTC, Learner wrote: On Thursday, 12 August 2021 at 10:10:17 UTC, rikki cattermole wrote: On 12/08/2021 9:36 PM, Learner wrote: It seems that there is no easy way to transition from a postblit to a copy constructor, no? struct Foo { this(ref Foo

Re: I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
On Thursday, 12 August 2021 at 11:32:03 UTC, Paul Backus wrote: On Thursday, 12 August 2021 at 11:19:34 UTC, drug wrote: ```D struct A { int[] data; this(ref return scope inout A rhs) /* no inout here */ { data = rhs.data.dup; } } ``` The problem is that if you qualify the ctor itself

Re: I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
On Thursday, 12 August 2021 at 11:07:24 UTC, drug wrote: 12.08.2021 12:36, Learner пишет: > It seems that there is no easy way to transition from a postblit to a copy constructor, no? You just need both const and mutable copy ctors to replace inout one: ```D struct A { int[] data;

Re: I do not understand copy constructors

2021-08-12 Thread drug via Digitalmars-d-learn
12.08.2021 14:32, Paul Backus пишет: This is not true. Qualifying the ctor as `inout` works fine: https://run.dlang.io/is/Kpzp5M The problem in this example is that `.dup` always returns a mutable array, even if the array being copied is `inout`. The solution is to cast the copy back to the

Re: I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
On Thursday, 12 August 2021 at 10:10:17 UTC, rikki cattermole wrote: On 12/08/2021 9:36 PM, Learner wrote: It seems that there is no easy way to transition from a postblit to a copy constructor, no? struct Foo { this(ref Foo other) { foreach(i, v; other.tupleof)

Re: I do not understand copy constructors

2021-08-12 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 12 August 2021 at 11:19:34 UTC, drug wrote: ```D struct A { int[] data; this(ref return scope inout A rhs) /* no inout here */ { data = rhs.data.dup; } } ``` The problem is that if you qualify the ctor itself then if you pass const/immutable rhs to it then the ctor is cons

Re: I do not understand copy constructors

2021-08-12 Thread drug via Digitalmars-d-learn
12.08.2021 14:07, drug пишет: 12.08.2021 12:36, Learner пишет:  > It seems that there is no easy way to transition from a postblit to a copy constructor, no? You just need both const and mutable copy ctors to replace inout one: ```D struct A {     int[] data;     this(ref return scope A

Re: I do not understand copy constructors

2021-08-12 Thread drug via Digitalmars-d-learn
12.08.2021 12:36, Learner пишет: > It seems that there is no easy way to transition from a postblit to a copy constructor, no? You just need both const and mutable copy ctors to replace inout one: ```D struct A { int[] data; this(ref return scope A rhs) { data = rhs.data.dup; }

Re: I do not understand copy constructors

2021-08-12 Thread rikki cattermole via Digitalmars-d-learn
On 12/08/2021 9:36 PM, Learner wrote: It seems that there is no easy way to transition from a postblit to a copy constructor, no? struct Foo { this(ref Foo other) { foreach(i, v; other.tupleof) this.tupleof[i] = v; } @disable th

Re: I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
On Thursday, 12 August 2021 at 09:14:02 UTC, Paul Backus wrote: On Thursday, 12 August 2021 at 08:42:27 UTC, Learner wrote: struct A { int[] data this(ref return scope A rhs) { data = ths.data.dup; } } Generating an `inout` copy constructor for `struct B` failed, th

Re: I do not understand copy constructors

2021-08-12 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 12 August 2021 at 08:42:27 UTC, Learner wrote: struct A { int[] data this(ref return scope A rhs) { data = ths.data.dup; } } Generating an `inout` copy constructor for `struct B` failed, therefore instances of it are uncopyable What is an `inout` copy

I do not understand copy constructors

2021-08-12 Thread Learner via Digitalmars-d-learn
I have a structure like, used by other structures: struct A { int[] data; this(this) { data = data.dup; } } I am trying to upgrade it to use copy constructor: struct A { int[] data this(ref return scope A rhs) { data = ths.data.dup; } } Gener