Re: Thread/Task cancellation

2023-08-01 Thread Gjiergji via Digitalmars-d-learn

On Friday, 28 July 2023 at 20:13:24 UTC, Ruby The Roobster wrote:

This is the only way I could think of doing this, since 
exceptions don't travel up the threads.


Thank you for pointing me in the right direction. I think that 
exceptions are not really necessary in D for cancellation, as I 
said, the code above was idiomatic C#.


Finally I come up with this code to achieve the same:

```d
void longRunningThread()
{
   while (true)
   {
if (receiveTimeout(3000.msecs, (bool cancel)  {}))
{
writeln("Cancelled");
break;
}
else
{
//do some work
writeln("Another 3 seconds passed");
}
   }
}

int main()
{
writeln("Press Enter to stop");
auto tid = spawn();
readln();
tid.send(true); //here we cancel
writeln("Press Enter to exit");
readln();
return 0;
}

```

Maybe someone add such examples to 
https://wiki.dlang.org/Programming_in_D_for_CSharp_Programmers 
which is anyway totally outdated.


Re: HTTP Post Body Parameters

2023-08-01 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/1/23 7:57 PM, Vahid wrote:

Hi,

I want to submit a request to server with "x-www-form-urlencoded" 
header. This is the simplified version of my code:



     auto http = HTTP("https://myurl.com/api;);
     http.addRequestHeader("Content-Type", 
"application/x-www-form-urlencoded");

     http.addRequestHeader("Authorization", "SID:TOKEN");

     auto params = "Param1=one=two";
     http.setPostData(params, "application/x-www-form-urlencoded");

     http.onReceive = (ubyte[] response)
     {
     return cast(string) response;
     };


onReceive is a push from the library to you. Returning the response 
doesn't actually do anything. You need to store it somewhere.


e.g.

```d
ubyte[] msg;
http.onReceive = (ubyte[] response) {
msg ~= response;
return response.length; // tell the receiver how much data was read
};

http.perform(); // fills in msg
return msg;
```
from the std.net.curl docs about onReceive (which by the way, should 
return a ulong, I'm not sure how your cast to string even compiles):


The event handler that receives incoming data. Be sure to copy the 
incoming ubyte[] since it is not guaranteed to be valid after the 
callback returns.


-Steve


Re: HTTP Post Body Parameters

2023-08-01 Thread Ivan Kazmenko via Digitalmars-d-learn

On Tuesday, 1 August 2023 at 23:57:29 UTC, Vahid wrote:
I want to submit a request to server with 
"x-www-form-urlencoded" header.


Isn't https://dlang.org/library/std/net/curl/post.html what you 
need?




HTTP Post Body Parameters

2023-08-01 Thread Vahid via Digitalmars-d-learn

Hi,

I want to submit a request to server with "x-www-form-urlencoded" 
header. This is the simplified version of my code:



auto http = HTTP("https://myurl.com/api;);
http.addRequestHeader("Content-Type", 
"application/x-www-form-urlencoded");

http.addRequestHeader("Authorization", "SID:TOKEN");

auto params = "Param1=one=two";
http.setPostData(params, "application/x-www-form-urlencoded");

http.onReceive = (ubyte[] response)
{
return cast(string) response;
};
http.perform();

This doesn't work as the server not receiving the parameters as 
post body. How can I send parameters as post body (or form data)?


This is the RAW CURL version that I want to write using D:

curl -X POST "https://myurl.com/api; \
--data-urlencode "Param1=one" \
--data-urlencode "Param2=two" \
-u SID:TOKEN


Re: how to make pragma(lib)'s path relative to the package's path?

2023-08-01 Thread ryuukk_ via Digitalmars-d-learn

On Monday, 31 July 2023 at 08:58:43 UTC, Johan wrote:

On Monday, 31 July 2023 at 00:32:07 UTC, ryuukk_ wrote:
I reworked the PR, here is the new link: 
https://github.com/dlang/dmd/pull/15479


It basically add support for ``pragma(lib, 
"local:bin/lib.a");``


Makes things easier, and doesn't change any old behavior


Before continuing with the PR, there should be a discussion and 
agreement about what exact behavior is desired. That is: get 
input and consensus from packagers.


cheers,
  Johan


I would like to get the PR merged asap, i'm already using that 
feature for my game, it greatly simplified my build script and 
i'm not willing to change back lol


How/Where do i kickstart this discussion?


Re: How can overloads be distinguished on attributes alone?

2023-08-01 Thread Quirin Schroll via Digitalmars-d-learn

On Monday, 31 July 2023 at 18:15:25 UTC, Jonathan M Davis wrote:
On Monday, July 31, 2023 4:55:44 AM MDT Quirin Schroll via 
Digitalmars-d-learn wrote:

Apparently, functions can be overloaded solely distinguished by
attributes:
```d
void f(ref int x) pure { x = 1; }
void f(ref int x)  { x = 2; static int s; ++s; }
```

I thought that, maybe, a `pure` context calls the `pure` 
function and an impure context calls the impure function, but 
no: Calling `f` leads to an ambiguity error in both contexts. 
Even if that worked, what about inferred contexts, i.e. 
templates? In simple cases, they could forward the contexts in 
which they are called, but you can instantiate a template 
without calling it.


What am I missing here?


As things stand, the context in which a function is called is 
irrelevant. All that matters is the arguments.


And actually, allowing it would complicate any functions that 
infer attributes, potentially in a way that wouldn't work. For 
instance, if you have a templated function that's trying to 
infer purity, which one should it call? If it calls the pure 
one, it could be pure, but if it doesn't, it can't be. Either 
way, because the context isn't yet pure or not, the context 
can't be used to determine which should be called. Potentially, 
the compiler could just choose the pure function in that case, 
but the problem gets worse as you add more attributes.


I reasoned like this up about this point.

For instance, what happens when you have a function that's pure 
but not @safe and one that's @safe but not pure?

```d
void f() pure {...}
void f() @safe {...}
```
Should the compiler favor calling the pure one or the @safe 
one? And what if you then add something to the function that 
isn't @safe? If it was calling the @safe version before, should 
it switch to the pure one? And if the functions were @safe pure 
and @system and not pure instead

```d
void f() @safe pure {...}
void f() @system {...}
```
then changing the @safety or purity of some of the other code 
in the templated function could result in the loss of both 
attributes. And the more attributes are involved, the more 
complex the situation gets.


I didn’t even consider multiple attributes “in competition”.
At this point, it’s obvious that this can’t work.

In effect, we'd be making the attribute inference process have 
to go in two directions instead of just going from the bottom 
up, with the added complication that it would potentially need 
to choose between sets of attributes when choosing which 
function overload to call.


I tried assigning the address to a function pointer to 
disambiguate which overload I want. Didn’t work.


It's not necessarily the case that we couldn't sort all of this 
out and come up with a clean set of rules that allowed 
functions that infer their attributes to call the correct 
function, but it does get pretty complicated, and it comes with 
the serious downside that there's no guarantee that the 
overloads even do something similar to one another.


Actually, I do think it’s impossible to do the right thing. The 
spec can only make guesses on what a programmer might want.


And when you consider that it's pretty easy for a change in one 
part of the code to change which attributes are inferred in 
another part of the code, you could easily end up having a 
change in one part of your program resulting in drastically 
different behavior in a seemingly unrelated part of your 
program. And even worse, that change could be because of a 
library update, making it that much less obvious which parts of 
your program could suddenly change behavior due to a change in 
attributes.


Before looking into this, I thought that maybe this was in fact 
intended.


And I'm probably forgetting other issues that this would add to 
the mix. So, while it may very well be possible to do something 
along the lines of what you're looking for, I strongly suspect 
that it's simply not worth it.


You might have gotten me wrong. I don’t want to do something with 
it, I wondered if overloading based on attributes is a thing one 
has to consider when writing templates or something like that. A 
simple test was: Can I define those? If so, what happens on a 
function call? The spec doesn’t say anything about it.


As you say, overloads should essentially do the same. Overloads 
differing in attributes would differ in implementation details 
such that one can make guarantees and the other might give you 
better performance or other guarantees. Maybe that’s enough such 
that, if both implementations have value, they should differ in 
name (or  a kind of tag parameter for overload selection).


Filed as https://issues.dlang.org/show_bug.cgi?id=24063