Thread overview
Am I missing with ref in this code?
Jan 24, 2019
Suliman
Jan 24, 2019
Paul Backus
Jan 24, 2019
Suliman
Jan 24, 2019
Paul Backus
Jan 24, 2019
bauss
Jan 25, 2019
Suliman
Jan 25, 2019
Suliman
Jan 26, 2019
Jesse Phillips
January 24, 2019
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?

January 24, 2019
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);
January 24, 2019
> 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*)'."
January 24, 2019
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.
January 24, 2019
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.

January 25, 2019
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?
January 25, 2019
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?
January 26, 2019
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.