www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Am I missing with ref in this code?

reply Suliman <evermind live.ru> writes:
I am doing very small link-checker. Here is' code 
https://run.dlang.io/is/p8whrA

I am expecting that on line:
writefln("url: %s, status: %s", url.url, url.status);

I will print link and it's status. But I am getting only:
url: http://127.0.0.1:8081/hck, status:
url: http://127.0.0.1:8081/hck2, status:
url: http://127.0.0.1:8081/hck3, status:

It's seems that I missed something with refs? Could you help me 
find error?
Jan 24 2019
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Thursday, 24 January 2019 at 15:28:19 UTC, Suliman wrote:
 I am doing very small link-checker. Here is' code 
 https://run.dlang.io/is/p8whrA

 I am expecting that on line:
 writefln("url: %s, status: %s", url.url, url.status);

 I will print link and it's status. But I am getting only:
 url: http://127.0.0.1:8081/hck, status:
 url: http://127.0.0.1:8081/hck2, status:
 url: http://127.0.0.1:8081/hck3, status:

 It's seems that I missed something with refs? Could you help me 
 find error?
It's because runWorkerTask internally passes its arguments along to the function by value: https://github.com/vibe-d/vibe.d/blob/master/core/vibe/core/core.d#L364 The workaround is to pass a pointer instead: void getServiceStatus(MyUrl* url) { // ... } // ... runWorkerTask(&getServiceStatus, &url);
Jan 24 2019
parent reply Suliman <evermind live.ru> writes:
 It's because runWorkerTask internally passes its arguments 
 along to the function by value:

 https://github.com/vibe-d/vibe.d/blob/master/core/vibe/core/core.d#L364

 The workaround is to pass a pointer instead:

     void getServiceStatus(MyUrl* url) {
         // ...
     }

     // ...

     runWorkerTask(&getServiceStatus, &url);
void getServiceStatus(MyUrl* url) { ... } Error: static assert: "Cannot convert arguments '(MyUrl)' to function arguments '(MyUrl*)'."
Jan 24 2019
parent reply Paul Backus <snarwin gmail.com> writes:
On Thursday, 24 January 2019 at 16:04:06 UTC, Suliman wrote:
  Error: static assert:  "Cannot convert arguments '(MyUrl)' to 
 function arguments '(MyUrl*)'."
You've forgotten to change the call site to pass a pointer. However, it turns out that even if you do that, vibe will not allow you to pass a pointer in this situation: .dub/packages/vibe-core-1.5.0/vibe-core/source/vibe/core/taskpool.d(114,21): Error: static assert: "Argument type MyUrl* is not safe to pass between threads." So, I'm not sure what the best solution here is.
Jan 24 2019
parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 24 January 2019 at 21:25:45 UTC, Paul Backus wrote:
 So, I'm not sure what the best solution here is.
The best solution is just to pass a copy since there's no absolute need for a reference to be passed.
Jan 24 2019
parent reply Suliman <evermind live.ru> writes:
On Thursday, 24 January 2019 at 22:02:36 UTC, bauss wrote:
 On Thursday, 24 January 2019 at 21:25:45 UTC, Paul Backus wrote:
 So, I'm not sure what the best solution here is.
The best solution is just to pass a copy since there's no absolute need for a reference to be passed.
But if I will pass copy how I can create array of structures with correct response status? Create new structures?
Jan 24 2019
parent Suliman <evermind live.ru> writes:
Do I need in my code `ref`s ? https://run.dlang.io/is/8dtkC7
Or if vibed doing copy under the hood `ref`s useless?
Jan 25 2019
prev sibling parent Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Thursday, 24 January 2019 at 15:28:19 UTC, Suliman wrote:
 I am doing very small link-checker. Here is' code 
 https://run.dlang.io/is/p8whrA

 I am expecting that on line:
 writefln("url: %s, status: %s", url.url, url.status);

 I will print link and it's status. But I am getting only:
 url: http://127.0.0.1:8081/hck, status:
 url: http://127.0.0.1:8081/hck2, status:
 url: http://127.0.0.1:8081/hck3, status:

 It's seems that I missed something with refs? Could you help me 
 find error?
You're writing asynchronous code and expecting it to process synchronized. Just make a synchronous call. If you need async use message passing with concurrency, probability on each call to the url so you can do the slow operations in parallel.
Jan 26 2019