Re: Is "auto t=T();" not the same as "T t;"?

2022-10-26 Thread Kagamin via Digitalmars-d-learn
Looks like explicitly initialized variable in this case allocates array literal. Uninitialized variable is initialized with init pattern. This may be correct as uninitialized variable isn't guaranteed to hold a value most useful for you, it's only guaranteed to hold a defined value.

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-26 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 26 October 2022 at 04:40:17 UTC, Salih Dincer wrote: On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote: Does the second piece of code shows a bug or my expectation is not correct (and why if so)? As a result, if this is a bug, Andrey has the right to report it.

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 26 October 2022 at 02:34:24 UTC, Ali Çehreli wrote: On 10/25/22 19:25, Salih Dincer wrote: > with static in main(): If 'static' makes a difference on your side as well, it is your turn to create a bug report. :) (Last time you discovered a bug, I was too quick to report it. :/)

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 19:25, Salih Dincer wrote: > with static in main(): If 'static' makes a difference on your side as well, it is your turn to create a bug report. :) (Last time you discovered a bug, I was too quick to report it. :/) Ali

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 26 October 2022 at 00:44:45 UTC, H. S. Teoh wrote: If you'll excuse some ASCII art, here's the situation you have: STACK GLOBAL DATA x[0] { Y[] y; -+> [ Y.init ] } | x[1] { |

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 17:16, Salih Dincer wrote: > Excuse me, but they still write in purple prose about dynamic > array literature here! I've heard positive things about D's arrays from people who use D in production. Especially slices... > I've known D for more than 10 years, but the topic we're

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Oct 26, 2022 at 12:16:55AM +, Salih Dincer via Digitalmars-d-learn wrote: [...] > I've known D for more than 10 years, but the topic we're talking about > still seems strange to me. The explanations given are not enough for > me, I'm sorry. > > Can anyone tell me what happens when I

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 20:36:28 UTC, matheus wrote: On int[] a = [1]; int[] b = a.dup; assert([0] !is [0]); // different memory ``` This is interesting, I understand the point of "reference vs copy", and I'm OK with this design choice of, but I wonder in the case of newcomers if

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 13:36, matheus wrote: > On Tuesday, 25 October 2022 at 20:12:25 UTC, Paul Backus wrote: >> Static arrays are value types. What that means is, when we say float[3], there are just 3 floats without any overhead. >> Dynamic arrays are reference types. That phrase can be confusing

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 20:27:18 UTC, Ali Çehreli wrote: On 10/25/22 13:12, Paul Backus wrote: > In order to create a copy of a static array Although .dup works for static arrays as well, you meant "dynamic array" and everyones knows it. :) Yes; thank you for the correction. :)

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread matheus via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 20:12:25 UTC, Paul Backus wrote: On Tuesday, 25 October 2022 at 17:54:16 UTC, Salih Dincer wrote: On Tuesday, 25 October 2022 at 17:18:35 UTC, Paul Backus wrote: It's not a bug. They're pointing to the exact same instance of `A` in memory: I don't understand?

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 13:12, Paul Backus wrote: > In order to create a copy of a static array Although .dup works for static arrays as well, you meant "dynamic array" and everyones knows it. :) > with its own block of > memory, separate from the original, you have to use the built-in `.dup` > method:

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 17:54:16 UTC, Salih Dincer wrote: On Tuesday, 25 October 2022 at 17:18:35 UTC, Paul Backus wrote: It's not a bug. They're pointing to the exact same instance of `A` in memory: I don't understand? So I don't understand why it causes problems with dynamic

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 11:23, Steven Schveighoffer wrote: >> Why do I say incorrect things like that? :) > You were right actually. As always! Now I'm confused. :o) Ali

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/25/22 2:03 PM, Ali Çehreli wrote: On 10/25/22 11:01, Ali Çehreli wrote: > static arrays don't have .ptr to point > to any member. Why do I say incorrect things like that? :) Of course static arrays have .ptr as well but that always point to their own body of N elements. They own

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 11:01, Ali Çehreli wrote: > static arrays don't have .ptr to point > to any member. Why do I say incorrect things like that? :) Of course static arrays have .ptr as well but that always point to their own body of N elements. They own their elements... I move away from the keyboard

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 10:54, Salih Dincer wrote: > So I don't understand why it causes problems with > dynamic arrays! So why is there nothing wrong with the static array in > the example below? The same rules as other uses of dynamic arrays... > //A[] a = [A.init];/* In that case, there is a

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 17:18:35 UTC, Paul Backus wrote: It's not a bug. They're pointing to the exact same instance of `A` in memory: I don't understand? So I don't understand why it causes problems with dynamic arrays! So why is there nothing wrong with the static array in the

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 08:50, Andrey Zherikov wrote: > I'd like to tune default ctor but structs can't have custom one. > Adding a ctor with parameter seems a hack to me There is static opCall, which may be seen as a hack as well. :) The following all print the same thing now: import std.stdio; struct

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 16:52:48 UTC, Salih Dincer wrote: On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote: Does the second piece of code shows a bug or my expectation is not correct (and why if so)? This is a bug: ```d void main() { struct B { struct A {

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote: Does the second piece of code shows a bug or my expectation is not correct (and why if so)? This is a bug: ```d void main() { struct B { struct A { int i = 10; } A[] a = [A.init]; } B[2] b;

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 14:53:50 UTC, Adam D Ruppe wrote: On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote: A[] a = [A.init]; This is a problem - this is referring to a static array instance, shared across all copies of B. You almost certainly don't want this.

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 14:53:50 UTC, Adam D Ruppe wrote: But just don't do this. Only basic values and immutable strings are good to initialize this way. With the array or class objects, you're liable to get some shared thing. If you change this to be initialized in a constructor

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 07:53, Adam D Ruppe wrote: > On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote: >> A[] a = [A.init]; > > This is a problem - this is referring to a static array instance, shared > across all copies of B. You almost certainly don't want this. Agreed. It should be

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Adam D Ruppe via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote: A[] a = [A.init]; This is a problem - this is referring to a static array instance, shared across all copies of B. You almost certainly don't want this. That B.a[0] is the *same object* across different