Obsecure problem 2

Ruby The Roobster rubytheroobster at yandex.com
Wed Aug 3 21:35:29 UTC 2022


On Wednesday, 3 August 2022 at 19:11:51 UTC, pascal111 wrote:
> On Wednesday, 3 August 2022 at 18:53:35 UTC, jfondren wrote:
>> On Wednesday, 3 August 2022 at 18:33:37 UTC, pascal111 wrote:
>>> I changed it to "x=notfunny(x);" and has the same result.
>>
>> Now you are changing the value of the temporary loop variable 
>> that is still immediately discarded afterwards.
>>
>> You should return a new range that has the values you want, 
>> not try to mutate the range you're given.
>
> You should provide a code instead of describing my mistakes!!

No need for a code.  See, there is a keyword called `ref`, that 
can be used both in function parameters and in foreach loops, and 
it is the equivalent of a pointer to the type specified, but it 
is automatically dereferenced. As arrays are themselves reference 
types, you don't need to change the parameter from `Range range` 
to `ref Range range`.

On the other hand, `int` in it of itself isn't a reference type, 
meaning that to modify the actual value, you need to change the 
foreach loop to be:

```d
foreach(ref x; range)
     x = notfunny(x);
```

Proof:  I tested it, and it works.


More information about the Digitalmars-d-learn mailing list