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

Ali Çehreli acehreli at yahoo.com
Tue Oct 25 22:13:16 UTC 2022


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 because we often use the terms "dynamic 
array" and "slice" interchangably.

I find the following simpler to understand (can still be confusing):

- Dynamic arrays are expandable arrays that are owned by the D runtime 
(the GC). Dynamic arrays don't have names.

- The names (symbols) that we see in source code are slices that are 
references to elements of arrays. Such arrays can be static or dynamic.

When we add an element to a slice that has no room (.capacity <= 
.length) then a fresh dynamic array is created from the copies of the 
elements of the slice.

 > if this is a case the generate more problem understanding this rules,

I think all these array complexities are inherent. Other examples from 
two other languages that I know:

- In C, arrays that are members of user-defined types are value types. 
Arrays that are parameters are reference types (pointer to the first 
element). Confusing.

- In C++, std::vector is a value type, which would be copied if passed 
by value. So there came std::array, std::string_view, std::span, etc. to 
address value versus reference complexities.

I don't think D could do anything better with arrays. They seem to work 
pretty well and are among the favorite features of many D programmers.

Ali



More information about the Digitalmars-d-learn mailing list