Referance usage in async function

aliak something at something.com
Sat Nov 30 15:52:53 UTC 2019


On Saturday, 30 November 2019 at 13:45:00 UTC, kerdemdemir wrote:
> I have simplified my problem which can be seen below.
>
> import std.stdio;
> import vibe.core.core;
> import vibe.core.concurrency;
> import vibe.data.json;
>
> void main()
> {
>     int[] list;
>
>     bool ListManipulator(ref int[] list)
>     {
>         list ~= 2;
>         list ~= 4;
>         return true;
>     }
>
>     bool ListManipulatorPointer( int[]* list)
>     {
>         *list ~= 2;
>         *list ~= 4;
>         return true;
>     }
>
>
>     auto future = vibe.core.concurrency.async(&ListManipulator, 
> list);
>     future.getResult();
>
>     writeln(list); ----> prints empty list
>
>     future = 
> vibe.core.concurrency.async(&ListManipulatorPointer, &list);
>     future.getResult();
>
>     writeln(list); ----> prints [2,4]
> }
>
>
> Why passing the pointer works meanwhile passing as reference 
> does nothing? I feel that is more D issue than vibe.d which I 
> can learn something I hope.
>
> Erdem

Looks like a bug in vibe: 
https://github.com/vibe-d/vibe-core/blob/master/source/vibe/core/concurrency.d#L1141

The function async doesn't use auto ref on ARGS. So the arguments 
are value copies through. If it used auto ref all the way through 
then it'd probably work:

---
import std;

void async(CALLABLE, ARGS...)(CALLABLE callable, auto ref ARGS 
args) {
     callable(args);
}

void main() {
     int[] list;
     bool ListManipulator(ref int[] list) {
         list ~= 2;
         list ~= 4;
         return true;
     }

     async(&ListManipulator, list);
     writeln(list);
}


More information about the Digitalmars-d-learn mailing list