Pointer vs Ref

confuzzled con at fuzzled.com
Wed Jun 11 17:26:22 UTC 2025


On 6/9/25 11:16 PM, monkyyy wrote:
> On Monday, 9 June 2025 at 07:24:41 UTC, confuzzled wrote:
>> Hello community,
>>
>> Is it possible to accomplish the following using ref instead of 
>> pointers? If so, please share an example.
>>
> 
> The hard part is the `foreach` the simple answer is throw a ref before 
> `val`.
> 
> Define your data to impliment ranges... but then you lose indexing on 
> foreach
> You can then use enumerate... but then you lose ref
> 
> Drop range entirely and you can interface with foreach with opApply, but 
> then you dont get map/reduce.
> 
> ---
> 
> Ranges do not support indexing in truth(enumerate is fundamentally 
> incorrect after a filter) and foreach is opinionated about ranges.
> 
> the tradeoffs as I understand them:
> 
> 1. use raw arrays, `foreach(i, ref v` is discouraged(there was a stupid 
> breaking change) but the community will fight tooth and nail on the subject
> 
> 2. make your own enumerate with a ref index(refness on tuples)
> 
> 3. define opApply with 2 arguments(delegates dont share the shared 
> refness issues of tuples)

Thank you very much monkyyy.


More information about the Digitalmars-d-learn mailing list