Pointer vs Ref

confuzzled con at fuzzled.com
Wed Jun 11 17:33:24 UTC 2025


On 6/10/25 2:17 AM, Ali Çehreli wrote:
> On 6/9/25 12:24 AM, confuzzled wrote:
> 
>  > Is it possible to accomplish the following using ref instead of
>  > pointers?
> 
> Your example is a good case for a pointer. Was there a reason why a 
> reference should be used?

Just exploring the realm of possibilities. I thought they could 
accomplish the same things as pointers but safer. And since we now have 
auto ref in the global space... well, by brain got away from me. Why is 
auto ref illegal inside UDTs anyway?

> Having said that, you can even drop the pointer and use a DataSource as-is:
> 
> struct Engine
> {
>      DataSource dataSource;
>      // ...
> }
> 
> However, that would be preferable only if DataSource did not have a 
> static array but I assume you wrote int[3] just as an example. Otherwise:

The size is an example but it is indeed a static array.

> struct DataSource
> {
>      int[] data;    // <-- Now a slice
> }
> 
> That works because DataSource.data is a *reference* to your external data.
> 
> Ali
> 

Thanks Ali


More information about the Digitalmars-d-learn mailing list