Re: Am I missing with ref in this code?

2019-01-26 Thread Jesse Phillips via Digitalmars-d-learn

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.


Re: Am I missing with ref in this code?

2019-01-25 Thread Suliman via Digitalmars-d-learn

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?


Re: Am I missing with ref in this code?

2019-01-24 Thread Suliman via Digitalmars-d-learn

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?


Re: Am I missing with ref in this code?

2019-01-24 Thread bauss via Digitalmars-d-learn

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.




Re: Am I missing with ref in this code?

2019-01-24 Thread Paul Backus via Digitalmars-d-learn

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.


Re: Am I missing with ref in this code?

2019-01-24 Thread Suliman via Digitalmars-d-learn
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(, );



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


 Error: static assert:  "Cannot convert arguments '(MyUrl)' to 
function arguments '(MyUrl*)'."


Re: Am I missing with ref in this code?

2019-01-24 Thread Paul Backus via Digitalmars-d-learn

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(, );


Am I missing with ref in this code?

2019-01-24 Thread Suliman via Digitalmars-d-learn
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?