Re: parallelism with delegate

2023-09-21 Thread user1234 via Digitalmars-d-learn
On Friday, 22 September 2023 at 04:33:44 UTC, Vitaliy Fadeev 
wrote:
On Friday, 22 September 2023 at 04:24:19 UTC, Vitaliy Fadeev 
wrote:

...


Skip this thread. I see solution.

How to delete missed posts on this forum ?


It's there forever, you have to live with that error ;)

See https://forum.dlang.org/help#about




Re: parallelism with delegate

2023-09-21 Thread Vitaliy Fadeev via Digitalmars-d-learn
On Friday, 22 September 2023 at 04:24:19 UTC, Vitaliy Fadeev 
wrote:

...


Skip this thread. I see solution.

How to delete missed posts on this forum ?


parallelism with delegate

2023-09-21 Thread Vitaliy Fadeev via Digitalmars-d-learn

able ?
how to use correctly?

```d
  import std.parallelism;

  auto async_task = task!fn( args );  // error
  // Error: no property 
`opCall` for type `app.A`, did you mean `new A`?


  async_task.executeInNewThread();
```

where

```d
auto a = new A();
auto fn = 

class A
{
void fn( string url )
{
// DO
}
}
```

Playground:
https://run.dlang.io/is/HvhtoP

gist:
https://gist.github.com/run-dlang/218b69e1afd79e5944ea10aa7ca61e1b




Re: change object class

2023-09-21 Thread Vitaliy Fadeev via Digitalmars-d-learn
On Friday, 22 September 2023 at 03:33:08 UTC, Vitaliy Fadeev 
wrote:
On Friday, 22 September 2023 at 02:51:10 UTC, Vitaliy Fadeev 
wrote:

...


the most correct

```
chip
  __vtbl ---+   // one of
  __monitor |
  id|
  name  |
|-> Chip   // init
| Sense()
| Draw()
|
|-> Chip_Hovered   // on mouseover
| Sense()
| Draw()
|
+-> Chip_Selected  // on mouseclick
  Sense()
  Draw()
```



Re: change object class

2023-09-21 Thread Vitaliy Fadeev via Digitalmars-d-learn
On Friday, 22 September 2023 at 02:51:10 UTC, Vitaliy Fadeev 
wrote:

...

```
 Chip
   id
   name
   Sense()
   Draw()
```

instance

```
 chip = new Chip();
```

compiled to

```
 chip
   __vtbl   -> Chip
   __monitor Sense()
   idDraw()
   name
```

I want

```
chip
  __vtbl --+
  id   |
  name |
   |-> Chip_Hovered
   | Sense()
   | Draw()
   |
   +-> Chip_Hovered
 Sense()
 Draw()
```



Re: change object class

2023-09-21 Thread Vitaliy Fadeev via Digitalmars-d-learn

On Thursday, 21 September 2023 at 18:19:47 UTC, Imperatorn wrote:
On Sunday, 17 September 2023 at 15:05:59 UTC, Vitaliy Fadeev 
wrote:

Hi!
I want to change a method ```Draw``` on a custom object when 
the ```MouseIn``` event occurs.
This is known as "Change State" of the object: ```Init``` -> 
```Hovered```.


[...]


Interesting, but why would you want to do it that way? 


Q & A.
You can check the logic.


How to increase the battery life of a smartphone?


Reducing operations.

How to change the state of a button widget, for example, on 
mouseover?


By changing the pointer to the Draw method.

In addition to Draw, the widget has a Sense method. How to 
replace all pointers to methods at once?


Replace the pointer with a class.

(It's like a change of state! Exactly Turing's State Machine.)

The object fields are the same. Behavior changes.

The contents of the fields remain in memory. Only the pointer to 
the method table changes.


Of course, this requires care and forethought.

Perhaps the risks of changing class can be reduced by performing 
additional checks.


Changing class is a convenient tool. I want to use it.



Re: std.file: read, readText and UTF-8 decoding

2023-09-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, September 21, 2023 9:29:17 AM MDT Uranuz via Digitalmars-d-learn 
wrote:
> Hello!
> I have some strange problem. I am trying to parse XML files and
> extract some information from it.
> I use library dxml for it by Jonathan M Davis. But I have a
> probleme that I have multiple  XML files made by different people
> around the world. Some of these files was created with Byte Order
> Mark, but some of them without BOM. dxml expects no BOM at the
> start of the string.
> At first I tried to read file with std.file.readText. Looks like
> it doesn't decode file at any way and doesn't remove BOM, so dxml
> failed to parse it then. This looks strange for me, because I
> expect that "text" function must decode data to UTF-8. Then I
> read that this behavior is documented at least:
> """
> ...However, no width or endian conversions are performed. So, if
> the width or endianness of the characters in the given file
> differ from the width or endianness of the element type of S,
> then validation will fail.
> """
> So it's OK. But I understood that this function "readText" is not
> usefull for me.
> So I tried to use plain "read" that returns "void[]". Problemmme
> is that I still don't understand which method I should use to
> convert this to string[] with proper UTF-8 decoding and remove
> BOM and etc.
> Could you help me, please to make some clearance.
> P.S. Function readText looks odd in std.file, because you cannot
> specify any encoding to decode this file. And logic how it
> decodes is unclear...

readText works great as long as you know that you're dealing with files with
a specific encoding and without a BOM (which is very often true when dealing
with text files on *nix systems where they're using UTF-8), but it's not so
great when you're reading files where you have no clue what their encoding
is going to be (and it's worse on Windows where they unfortunately are much
more likely to be UTF-16). Phobos does give you the tools to solve the
problem, but it doesn't currently make it as easy as it arguably should be.
std.encoding has the pieces that you're missing here.

https://dlang.org/phobos/std_encoding.html#BOM
https://dlang.org/phobos/std_encoding.html#getBOM

You'll need to do something like

import std.encoding : BOM, getBOM;
import std.file : read;

auto data = read(file);
immutable bom = getBOM(cast(ubyte[])data).schema;

to get the BOM. Then you can compare the BOM against BOM.utf8, BOM.utf16le,
etc. so that you know what type to cast the data array to (string, wstring,
etc.). Then you can remove the BOM with something like

R stripBOM(R)(R range)
if(isForwardRange!R && isSomeChar!(ElementType!R))
{
import std.utf : decodeFront, UseReplacementDchar;
if(range.empty)
return range;
auto orig = range.save;
immutable c = range.decodeFront!(UseReplacementDchar.yes)();
return c == '\uFEFF' ? range : orig;
}

And then you either operate on the array with its current encoding type,
convert it to the desired string type (e.g. to!string) or wrap it in a type
that converts it as you parse it (e.g. std.utf.byChar).

Alternatively, you can just read the very beginning of the file and grab the
BOM that way and then call readText with the correct type after you've
figured out the file's encoding.

readText should currently handle the BOM correctly insofar as it checks
whether you made the correct choice when you told it whether you wanted a
string, wstring, etc., but since it reads in the entire file, it's not a
great plan to try it with each encoding (catching each exception in turn)
until you get the right one, and it doesn't strip the BOM off for you.

So, Phobos probably should get some new functionality to handle this better,
but it's at least possible to make it work with what's there.

- Jonathan M Davis





Re: change object class

2023-09-21 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 17 September 2023 at 15:05:59 UTC, Vitaliy Fadeev 
wrote:

Hi!
I want to change a method ```Draw``` on a custom object when 
the ```MouseIn``` event occurs.
This is known as "Change State" of the object: ```Init``` -> 
```Hovered```.


[...]


Interesting, but why would you want to do it that way? 


Re: Detect 8-bit alligned type TXY by TX,TY.

2023-09-21 Thread Imperatorn via Digitalmars-d-learn
On Tuesday, 19 September 2023 at 06:41:49 UTC, Vitaliy Fadeev 
wrote:
On Tuesday, 19 September 2023 at 06:33:25 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

[...]


Thank, Richard.

```.offsetof...``` mmm...

May be exists some like:

```d
// TXYXY = Detect!(TX,TX)
// Detect!(uint,uint) == ulong
template Detect(TX,TY)
{
static if ( TX.sizeof + TY.sizeof <= 8 )
alias Detect = ubyte;
else
static if ( TX.sizeof + TY.sizeof <= 16 )
alias Detect = ushort;
else
static if ( TX.sizeof + TY.sizeof <= 32 )
alias Detect= uint;
else
static if ( TX.sizeof + TY.sizeof <= 64 )
alias Detect= ulong;
else
static if ( TX.sizeof + TY.sizeof <= 128 )
alias Detect= ucent;
else
static assert( 0, "Expected size TX+TY <= 128" );
}
```


Do a mixin


Re: C to D: please help translate this weird macro

2023-09-21 Thread Imperatorn via Digitalmars-d-learn

On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote:

Here is the macro:

```C
#define NK_CONTAINER_OF(ptr,type,member)\
(type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - 
NK_OFFSETOF(type, member)))

```

I'm trying to translate the Nuklear GUI library to D 
[here](https://github.com/rillki/nuklear-d/tree/nuklear-d-translation).


When you're done, will you put it on dub?


Re: C to D: please help translate this weird macro

2023-09-21 Thread user1234 via Digitalmars-d-learn
On Thursday, 21 September 2023 at 16:28:25 UTC, Nick Treleaven 
wrote:

(Untested)


There might be a `need this` error




Re: C to D: please help translate this weird macro

2023-09-21 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 21 September 2023 at 16:28:25 UTC, Nick Treleaven 
wrote:

return cast(T*)(cast(void*)(cast(char*)ptr -
__traits(getMember, T, member).offsetof)));


There's a trailing `)` that needs removing. Also pretty sure it 
can be simplified to:


return cast(T*)(cast(char*)ptr -
__traits(getMember, T, member).offsetof);


Re: C to D: please help translate this weird macro

2023-09-21 Thread Nick Treleaven via Digitalmars-d-learn

On Thursday, 21 September 2023 at 02:57:07 UTC, Ki Rill wrote:

On Thursday, 21 September 2023 at 02:23:32 UTC, Ki Rill wrote:

wrote:

[...]


Translated it to this eventually:
```D
auto nk_container_of(P, T)(P ptr, T type, const(char)* member)
{
return cast(T*)(cast(void*)(cast(char*)
(ptr - __traits(getMember, type, member).offsetof)));
}
```


The 1st argument of `getMember` can just be T, like the original 
macro.

The 2nd argument needs to be a compile-time string.
Also the `char*` cast needs to apply to `ptr` before subtracting 
the offset AFAICS.


So reordering to keep type inference of `ptr`:
```d
auto nk_container_of(T, string member, P)(P ptr)
{
return cast(T*)(cast(void*)(cast(char*)ptr -
__traits(getMember, T, member).offsetof)));
}
```
(Untested)


Re: std.file: read, readText and UTF-8 decoding

2023-09-21 Thread Uranuz via Digitalmars-d-learn

Addition:
Current solution to this problemme that I was found is:
So I just check for BOM manually. Get length of bom.sequence and 
remove that count of items from beginning. But I dont' think that 
it's convenient solution, because `who knows` how much else 
issues with UTF could happend. And I don't think that it's 
correct to handle them on the side of users of standart D 
library... I think that should be solution "out of the box". It 
could be not much effective, but it should at least "just work" 
without extra movements...

string[] getGroupsFromFile(string groupFilePath)
{
writeln(`Parse file` ~ groupFilePath);
string[] groupNames = [];
char[] rawContent = cast(char[]) read(groupFilePath);
auto bom = getBOM(cast(ubyte[]) rawContent);
string content = cast(string) rawContent[bom.sequence.length..$];
writeln(`Content:\n` ~ content);

//... work with XML
return groupNames;
}



std.file: read, readText and UTF-8 decoding

2023-09-21 Thread Uranuz via Digitalmars-d-learn

Hello!
I have some strange problem. I am trying to parse XML files and 
extract some information from it.
I use library dxml for it by Jonathan M Davis. But I have a 
probleme that I have multiple  XML files made by different people 
around the world. Some of these files was created with Byte Order 
Mark, but some of them without BOM. dxml expects no BOM at the 
start of the string.
At first I tried to read file with std.file.readText. Looks like 
it doesn't decode file at any way and doesn't remove BOM, so dxml 
failed to parse it then. This looks strange for me, because I 
expect that "text" function must decode data to UTF-8. Then I 
read that this behavior is documented at least:

"""
...However, no width or endian conversions are performed. So, if 
the width or endianness of the characters in the given file 
differ from the width or endianness of the element type of S, 
then validation will fail.

"""
So it's OK. But I understood that this function "readText" is not 
usefull for me.
So I tried to use plain "read" that returns "void[]". Problemmme 
is that I still don't understand which method I should use to 
convert this to string[] with proper UTF-8 decoding and remove 
BOM and etc.

Could you help me, please to make some clearance.
P.S. Function readText looks odd in std.file, because you cannot 
specify any encoding to decode this file. And logic how it 
decodes is unclear...


Re: "readln" after "readf"

2023-09-21 Thread pascal111 via Digitalmars-d-learn
On Thursday, 21 September 2023 at 11:30:02 UTC, Steven 
Schveighoffer wrote:

On Thursday, 21 September 2023 at 09:14:14 UTC, pascal111 wrote:

[...]


Your readfs are not consuming the newline at the end. Add `\n` 
to the end of the format text.


-Steve


It works fine now, thanks!


Re: "readln" after "readf"

2023-09-21 Thread Steven Schveighoffer via Digitalmars-d-learn

On Thursday, 21 September 2023 at 09:14:14 UTC, pascal111 wrote:
I've a problem when I'm using "readln" after "readf" that I 
couldn't see my program rest and the lines of the execution ran 
fast:


module main;

import std.stdio;
import std.string;
import std.conv;

int main(string[] args)
{

char[] yy;
int x,y,z;


writef("Enter a number: ");
readf(" %d",);


writef("Enter a number: ");
readf(" %d",);

z=x+y;
writefln("Hi! %d",z);

writef("Enter a number: ");
readln(yy);
yy = strip(yy);
x=to!int(yy);

writef("Enter a number: ");
readln(yy);
yy = strip(yy);
y=to!int(yy);

z=x+y;
writefln("Hi! %d",z);

return 0;
}


Your readfs are not consuming the newline at the end. Add `\n` to 
the end of the format text.


-Steve



"readln" after "readf"

2023-09-21 Thread pascal111 via Digitalmars-d-learn
I've a problem when I'm using "readln" after "readf" that I 
couldn't see my program rest and the lines of the execution ran 
fast:


module main;

import std.stdio;
import std.string;
import std.conv;

int main(string[] args)
{

char[] yy;
int x,y,z;


writef("Enter a number: ");
readf(" %d",);


writef("Enter a number: ");
readf(" %d",);

z=x+y;
writefln("Hi! %d",z);

writef("Enter a number: ");
readln(yy);
yy = strip(yy);
x=to!int(yy);

writef("Enter a number: ");
readln(yy);
yy = strip(yy);
y=to!int(yy);

z=x+y;
writefln("Hi! %d",z);

return 0;
}