Re: Background thread, async and GUI (dlangui)

2022-07-08 Thread Ali Çehreli via Digitalmars-d-learn

On 7/8/22 06:32, Bagomot wrote:

> I do as in Ali's example, but
> the GUI is still blocked:

I don't know exactly why but something looks suspicious.

> ```d
> auto btn = new Button("Run"d);
> btn.click = delegate(Widget src) {
>  auto request = Request("dlang.org");

That is a local variable which will die after the return statement...

>  auto downloadTask = task!download();

... and that pointer will be invalid, still pointing on function call stack.

At least, I would move the 'auto request' line up, next to 'btn' so that 
it lives long and that you can get the 'result' later on.


But then, you need access to 'task' as well so that you can ensure it 
has finished with downloadTask.yieldForce().


>  downloadTask.executeInNewThread;
>  return true;
> };

Aside: What does the return value 'true' mean?

I am not sure whether my explicit way of starting a thread (albeit with 
std.parallelism) is the right way here. As you've been suspecting, 
dlangui may provide async execution. (?)


Ali



Is there any implementation of a 128bit integer?

2022-07-08 Thread Rob T via Digitalmars-d-learn

https://forum.dlang.org/post/mailman.10914.1566237225.29801.digitalmars-d-le...@puremagic.com

In case someone comes across this old thread

https://dlang.org/phobos/core_int128.html




Re: Background thread, async and GUI (dlangui)

2022-07-08 Thread Bagomot via Digitalmars-d-learn

On Friday, 8 July 2022 at 08:04:45 UTC, Bagomot wrote:

On Thursday, 7 July 2022 at 18:26:15 UTC, Ali Çehreli wrote:

Summing up your help and GrimMaple, I understood how to make 
the file download progress bar beautifully. Thank you!


I only have a question with how I can work with the GUI from 
other threads. For example, in my application I have a button 
(GUI) that starts a long running job. As I understand it, it 
should run it in a separate thread so as not to block the GUI.


I did it through Thread, but now I realized that there are more 
convenient ways through Task.


But I do not understand how I can manage the GUI from that 
separate task. For example, I start the task with the button 
and block the button press (without blocking the entire GUI), 
it does its job and unblocks the button.


This is where help is needed. How is it fundamentally correct 
to do this in D, and how to do it in general with dlangui?


Let's even look at the example of downloading a file.

Here in my application I have a button and its click event. I can 
set it to work on click. But I do not know how to do it so that 
the GUI does not block for the duration of the work. I do as in 
Ali's example, but the GUI is still blocked:

```d
auto btn = new Button("Run"d);
btn.click = delegate(Widget src) {
auto request = Request("dlang.org");
auto downloadTask = task!download();
downloadTask.executeInNewThread;
return true;
};
```
How to do it right? Not only for downloading, but for any 
background work in dlangui? Maybe someone knows?


Re: Cannot copy void[] to void[] in @safe code?

2022-07-08 Thread wjoe via Digitalmars-d-learn

On Friday, 8 July 2022 at 12:26:03 UTC, ag0aep6g wrote:
You're allowed to copy from `ubyte[]` to `ubyte[]`. But you're 
not allowed to copy from `ubyte[]` to `int*[]`, because 
reinterpreting a bunch of bytes as pointers is not safe.


The thing about `void[]` is that it can point to memory that 
also has a typed alias. You can have a `void[]` and a `ubyte[]` 
pointing to the same memory. And you can have a `void[]` and an 
`int*[]` pointing to the same memory. So if you were allowed to 
copy from `void[]` to `void[]`, you'd practically be allowed to 
copy from `ubyte[]` to `int*[]`. But that's not safe.



That makes a lot of sense. Thanks!



Re: Unwrap variadic template into vararg of pointers of the same types

2022-07-08 Thread wjoe via Digitalmars-d-learn

Corrections:

On Friday, 8 July 2022 at 12:40:52 UTC, wjoe wrote:

alias Recurse = AliasSeq!(Arg[0]*, Recurse!(Arg[0..$]);


```d
 alias Recurse = AliasSeq!(Arg[0]*, Recurse!(Arg[1..$]);
```

void view_it(Args...)(void function(entity_t, Includes!(Args) )


```d
void view_it(Args...)(void function(entity_t, Includes!(Args) 
cb)) {...}

```



Re: Unwrap variadic template into vararg of pointers of the same types

2022-07-08 Thread Paul Backus via Digitalmars-d-learn

On Friday, 8 July 2022 at 12:20:13 UTC, ryuukk_ wrote:
The problem when i try to introduce variadic template, is i 
can't seem to understand how to unwrap the parameter as pointer 
type T -> T*



```D
struct Includes(Args...) { alias args = Args; }

void view_it(Includes)(void function(entity_t, Includes.args* ) 
cb)

{
// do stuff
}

```

I get the following:

```Error: cannot have pointer to `(EEntityRemoved)```


You can use [`std.meta.staticMap`][1] to make each type into a 
pointer individually:


```d
import std.meta;

alias Args = AliasSeq!(int, string, double);
struct Includes { alias args = Args; }
struct entity_t {}

alias PointerTo(T) = T*;

void view_it(void function(entity_t, staticMap!(PointerTo, 
Includes.args) ) cb)

{
// do stuff
}
```

However, this will probably not work very well in your original 
code where `view_it` is a template, because the presence of 
`staticMap` in the parameter list will prevent the compiler from 
automatically deducing the template parameters.


So, in this case, I think a better solution is to make the type 
of the callback fully generic, and use a template constraint to 
enforce your requirements:


```d
import std.meta: allSatisfy;
import std.traits: isPointer;

void view_it(Callback)(Callback cb)
if (
is(Callback == void function(entity_t, Args), Args...)
&& allSatisfy!(isPointer, Args)
)
{
// do stuff
}
```

[1]: http://phobos.dpldocs.info/std.meta.staticMap.html


Re: Unwrap variadic template into vararg of pointers of the same types

2022-07-08 Thread wjoe via Digitalmars-d-learn

On Friday, 8 July 2022 at 12:20:13 UTC, ryuukk_ wrote:

I'm not sure how to phrase it so it'll try with code

I have this piece of code that i would like to improve, right 
now i have to create bunch of duplicates


```D
void view_it(A, B)(void function(entity_t, A*, B*) cb)
{
foreach(it, e; view!(Includes!(A, B)))
{
auto a = it.get!(A)(e);
auto b = it.get!(B)(e);
cb(e, a, b);
}
}
```

The problem when i try to introduce variadic template, is i 
can't seem to understand how to unwrap the parameter as pointer 
type T -> T*



```D
struct Includes(Args...) { alias args = Args; }

void view_it(Includes)(void function(entity_t, Includes.args* ) 
cb)

{
// do stuff
}

```

I get the following:

```Error: cannot have pointer to `(EEntityRemoved)```

Anyone got an idea?

Thanks!


I suppose you could do something like this:

```d
template Includes(Args...)
{
   template Recurse(Arg...)
   {
  import std.meta: AliasSeq;
  static if (1 == Arg.length)
alias Recurse = AliasSeq!(Arg[0]*);
  else
alias Recurse = AliasSeq!(Arg[0]*, Recurse!(Arg[0..$]);
   }

   alias Includes = Includes!(Args);
}
void view_it(Args...)(void function(entity_t, Includes!(Args) )
```


Re: Cannot copy void[] to void[] in @safe code?

2022-07-08 Thread ag0aep6g via Digitalmars-d-learn

On Friday, 8 July 2022 at 10:58:24 UTC, wjoe wrote:
My understanding is that a void[] doesn't have a distinct type 
but since the length is bytes and not elements this makes me 
believe that under the hood they are byte arrays - or, rather, 
managed chunks of memory.
How's copying memory without a distinct type different from 
copying, say, an ubyte[] to an ubyte[] ?


You're allowed to copy from `ubyte[]` to `ubyte[]`. But you're 
not allowed to copy from `ubyte[]` to `int*[]`, because 
reinterpreting a bunch of bytes as pointers is not safe.


The thing about `void[]` is that it can point to memory that also 
has a typed alias. You can have a `void[]` and a `ubyte[]` 
pointing to the same memory. And you can have a `void[]` and an 
`int*[]` pointing to the same memory. So if you were allowed to 
copy from `void[]` to `void[]`, you'd practically be allowed to 
copy from `ubyte[]` to `int*[]`. But that's not safe.


In code:

```d
void main() @safe
{
ubyte[] some_bytes = [1, 2, 3, 4, 5, 6, 7, 8];
int*[] some_pointers = [new int];
void[] v1 = some_bytes;
void[] v2 = some_pointers;
v2[] = v1[]; /* creating invalid pointers */
int x = *some_pointers[0]; /* undefined behavior */
}
```


Unwrap variadic template into vararg of pointers of the same types

2022-07-08 Thread ryuukk_ via Digitalmars-d-learn

I'm not sure how to phrase it so it'll try with code

I have this piece of code that i would like to improve, right now 
i have to create bunch of duplicates


```D
void view_it(A, B)(void function(entity_t, A*, B*) cb)
{
foreach(it, e; view!(Includes!(A, B)))
{
auto a = it.get!(A)(e);
auto b = it.get!(B)(e);
cb(e, a, b);
}
}
```

The problem when i try to introduce variadic template, is i can't 
seem to understand how to unwrap the parameter as pointer type T 
-> T*



```D
struct Includes(Args...) { alias args = Args; }

void view_it(Includes)(void function(entity_t, Includes.args* ) 
cb)

{
// do stuff
}

```

I get the following:

```Error: cannot have pointer to `(EEntityRemoved)```

Anyone got an idea?

Thanks!



Cannot copy void[] to void[] in @safe code?

2022-07-08 Thread wjoe via Digitalmars-d-learn

Why is that ?
My understanding is that a void[] doesn't have a distinct type 
but since the length is bytes and not elements this makes me 
believe that under the hood they are byte arrays - or, rather, 
managed chunks of memory.
How's copying memory without a distinct type different from 
copying, say, an ubyte[] to an ubyte[] ?


The compiler doesn't complain about that in @safe code:

```d
@safe void copy_voidA_to_ubyteA(in void[] s) {
  ubyte[] d;
  d.length = s.length;
  d[0..$] = cast(const ubyte[])s[0..$];
}
copy_voidA_to_ubyteA([1,2,3,4]);
```

But what if I copy pointers into an ubyte[] ?
void[] are scanned by the GC but ubyte[] aren't.


Re: How to call a function from a dll created with d ?

2022-07-08 Thread frame via Digitalmars-d-learn

On Thursday, 7 July 2022 at 17:29:42 UTC, cc wrote:

Does importing dimedll into app.d properly NOT link in the 
functions that are exported to the DLL?  When I tried something 
similar with dmd, I had to create a .di file containing just 
stubs, otherwise it looked like it was ignoring the DLL and 
compiling in an additional copy of each fuction.


IMHO this is the expected behaviour since the compiler comes 
before the linker and `pragma(lib)` is a linker directive, so if 
dimedll.di is not present it looks for dimedll.d instead and just 
compiles the code.


The link to DLL is only present in the actual lib-file and only 
used by the linker.


The pitfall with DMD is that the automatic generated di-file is 
not usable as it contains a mixin that is meant for the DLL but 
not the application that links it, resulting in _ModuleInfoZ 
error while trying.


Re: Background thread, async and GUI (dlangui)

2022-07-08 Thread Bagomot via Digitalmars-d-learn

On Thursday, 7 July 2022 at 18:26:15 UTC, Ali Çehreli wrote:

Summing up your help and GrimMaple, I understood how to make the 
file download progress bar beautifully. Thank you!


I only have a question with how I can work with the GUI from 
other threads. For example, in my application I have a button 
(GUI) that starts a long running job. As I understand it, it 
should run it in a separate thread so as not to block the GUI.


I did it through Thread, but now I realized that there are more 
convenient ways through Task.


But I do not understand how I can manage the GUI from that 
separate task. For example, I start the task with the button and 
block the button press (without blocking the entire GUI), it does 
its job and unblocks the button.


This is where help is needed. How is it fundamentally correct to 
do this in D, and how to do it in general with dlangui?