Re: dChar Error

2022-12-30 Thread Salih Dincer via Digitalmars-d-learn

On Saturday, 31 December 2022 at 02:15:56 UTC, Ali Çehreli wrote:

On 12/30/22 17:22, Salih Dincer wrote:

> I guess there is no other way but to overload.

Since the bodies of all three overloads are the same except 
some types, they can easily be templatized.


You took the trouble, thanks, but there is a special reason why I 
use union.  If we want, we can write dynamic version without 
using std.traits:


```d
struct Values(T, size_t len = 1)
{
  union
  {
T[len] data;
ubyte[T.sizeof * len] bytes;
  }

  string toString()
  {
import std.format;
return format("%s: %(%02X-%)", data, bytes);
  }
}

  alias Char = Values!char;
  alias Wchar = Values!wchar;
  alias Dchar = Values!dchar;

void main()
{
  import std.stdio : writeln;

  Char[] str1;
  str1 ~= Char('B');
  str1 ~= Char('E');
  str1 ~= Char('$');
  str1.writeln;

  Wchar[] str2;
  str2 ~= Wchar('β');
  str2 ~= Wchar('€');
  str2 ~= Wchar('Ş');
  str2.writeln;

  Dchar[] str3;
  str3 ~= Dchar('β');
  str3 ~= Dchar('€');
  str3 ~= Dchar('$');
  str3.writeln("\n");

  Fun!"β€$".writeln;
}
/*
  [B: 42, E: 45, $: 24]
  [β: B2-03, €: AC-20, Ş: 5E-01]
  [β: B2-03-00-00, €: AC-20-00-00, $: 24-00-00-00]

  β€$: CE-B2-E2-82-AC-24
*/
```
However, I would like to draw your attention to the last line.  
Yeah, I won't be able to do that because it's New Year's Eve.  
But the line is like mixed mode because of the non-char data it 
contains, right?


Happy New Year...



Re: Handling CheckBox state changes in DLangUI

2022-12-30 Thread Daren Scot Wilson via Digitalmars-d-learn

On Saturday, 31 December 2022 at 03:05:45 UTC, brianush1 wrote:
On Saturday, 31 December 2022 at 02:40:49 UTC, Daren Scot 
Wilson wrote:

The compiler errors I get are, for no '&' and with '&':

Error: function `app.checkbox_b_clicked(Widget source, bool 
checked)` is not callable using argument types `()`


Error: none of the overloads of `opAssign` are callable using 
argument types `(bool function(Widget source, bool checked))`


Try:

import std.functional : toDelegate;
check_c.checkChange = toDelegate(_b_clicked);


That works :)



Re: Handling CheckBox state changes in DLangUI

2022-12-30 Thread brianush1 via Digitalmars-d-learn
On Saturday, 31 December 2022 at 02:40:49 UTC, Daren Scot Wilson 
wrote:

The compiler errors I get are, for no '&' and with '&':

Error: function `app.checkbox_b_clicked(Widget source, bool 
checked)` is not callable using argument types `()`


Error: none of the overloads of `opAssign` are callable using 
argument types `(bool function(Widget source, bool checked))`


Try:

import std.functional : toDelegate;
check_c.checkChange = toDelegate(_b_clicked);


Handling CheckBox state changes in DLangUI

2022-12-30 Thread Daren Scot Wilson via Digitalmars-d-learn
I'm writing a GUI program using dlangui. It has some checkboxes. 
I'm trying to figure out how to invoke a callback function when 
the user clicks the box. What are the valid ways of doing that?


I can copy from dlangide's source, where a delegate is defined 
in-line and assigned. That seems to work.  But is that the only 
way?



bool g_x = true;

bool checkbox_b_clicked(Widget source, bool checked)
{
  g_x = checked;
  if (checked) {
  writeln(checked);
  }
  return true;
}


auto check_a = new CheckBox("wantalt", "Alternating"d);
auto check_b = new CheckBox("wantblinkb", "Blink(delg)"d);
auto check_c = new CheckBox("wantblinkc", 
"Blink(direct)"d);


check_a.checkChange = delegate(Widget w, bool checked) {
 g_x=checked;
 return true;
 };
check_b.checkChange = delegate(Widget w, bool checked) {
 return checkbox_b_clicked(w,checked);
 };
check_c.checkChange = checkbox_b_clicked;
check_c.checkChange = _b_clicked;

The assignment to check_a is fine with the compiler.

For check_b, I try calling a function defined earlier. (Maybe in 
real life it's too complex to try having inline.)  It was giving 
a compiler error until I realized I'm dumb, wasn't passing 'w' 
and 'checked' to it. Fixed, works fine now. Okay!


But what I think I should be able to do: assign 
checkbox_b_clicked directly to the .checkChange property of the 
checkbox, as shown for check_c.  It doesn't work. Oh, I see an 
example where '&' is used - okay let's try that... nope!


The compiler errors I get are, for no '&' and with '&':

Error: function `app.checkbox_b_clicked(Widget source, bool 
checked)` is not callable using argument types `()`


Error: none of the overloads of `opAssign` are callable using 
argument types `(bool function(Widget source, bool checked))`





Re: dChar Error

2022-12-30 Thread Ali Çehreli via Digitalmars-d-learn

On 12/30/22 17:22, Salih Dincer wrote:

> I guess there is no other way but to overload.

Since the bodies of all three overloads are the same except some types, 
they can easily be templatized.


> This is both the safest and the fastest.

I didn't think Values is fast with string copies that it makes. ;) I 
think it was only for showing the byte values but you can do the same by 
casting to ubyte[] as well.


Also, your Fun could work only with string literals; so I used function 
parameters.


import std.traits : isSomeString;

// isSomeString may or may not be useful below. (?)

auto Fun(S)(S str)
if (isSomeString!S) {
import std.traits : Unqual;
import std.conv : to;

alias T = Unqual!S;

// Note: The following may or may not copy the string
//   depending on whether S is the same as T.
return str.to!T;
}

void printBytes(S)(S str) {
import std.stdio : writefln;
import std.conv  : to;

// The following cast does not copy anything.
writefln!"%(%02X-%)"(cast(ubyte[])str);
}

void main()
{
printBytes(Fun("β€Ş"));  // CE-B2-E2-82-AC-C5-9E
printBytes(Fun("β€Ş"w)); // B2-03-AC-20-5E-01
printBytes(Fun("β€Ş"d)); // B2-03-00-00-AC-20-00-00-5E-01-00-00
}

Ali



Re: dChar Error

2022-12-30 Thread Salih Dincer via Digitalmars-d-learn

On Saturday, 31 December 2022 at 00:42:50 UTC, Salih Dincer wrote:

... it possible to infer


Let me save you the torment of code duplication 

Thanks everyone.  Yes, I guess there is no other way but to 
overload.  This is both the safest and the fastest.  It's also 
short enough like this:


```d
  // D 2.0.83 or higher

  import std.stdio : writeln;
  import std.conv  : to;

auto Fun(string str)() {
  auto result = Values!(char, str.length)();
  result.data = str.to!(char[]);
  return result;
}

auto Fun(wstring str)() {
  auto result = Values!(wchar, str.length)();
  result.data = str.to!(wchar[]);
  return result;
}

auto Fun(dstring str)() {
  auto result = Values!(dchar, str.length)();
  result.data = str.to!(dchar[]);
  return result;
}

struct Values(T, size_t len) {
  union {
T[len] data;
ubyte[T.sizeof * len] bytes;
  }
  string toString() {
import std.format;
return format("%s: %(%02X-%)", data, bytes);
  }
}

void main()
{
  Fun!"β€Ş".writeln;  // β€Ş: CE-B2-E2-82-AC-C5-9E
  Fun!"β€Ş"w.writeln; // β€Ş: B2-03-AC-20-5E-01
  Fun!"β€Ş"d.writeln; // B2-03-00-00-AC-20-00-00-5E-01-00-00
}
```

SDB@79




Re: dChar Error

2022-12-30 Thread Salih Dincer via Digitalmars-d-learn

On Friday, 30 December 2022 at 22:02:41 UTC, Ali Çehreli wrote:

> But I couldn't find if the target will be mutable, but I
think it will
> be,

The target will always be the type the programmer specifies 
explicitly. (dchar[] in this case.)


I have one more little question! Is it possible to infer the 
string without the type the programmer specifies explicitly in 
the template below?


```d

template Fun(dstring str)/*
template Fun(T)(T str)/* like this */
{
  import std.traits : Unconst;
  alias T = Unconst!(typeof(str[0]));

  auto Fun()
  {
import std.conv : to;

auto result = Result();
    result.data = str.to!(T[]);

    return result;
  }
  
  struct Result
  {
union
{
  T[str.length] data;
  ubyte[T.sizeof * str.length] bytes;
}
string toString()
{
  import std.format;
  return format("%s: %(%02X-%)", data, bytes);
}
  }
}

void main()
{
  import std.stdio : writeln;
  Fun!"β€Ş"w.writeln; // type1: wstring
  Fun!"β€Ş"d.writeln; // type2: dstring
}
```
That is, the compiler can/must infer between type 1-2 or type 
3(string) that it knows at compile time, right?


SDB@79


Re: dChar Error

2022-12-30 Thread matheus via Digitalmars-d-learn

On Friday, 30 December 2022 at 22:02:41 UTC, Ali Çehreli wrote:

On 12/30/22 13:54, matheus wrote:

> But yes I think it will generate a copy (mutable) based on
this test:

In this case it does copy but in the case of dchar[] to 
dchar[], there will be no copy. Similarly, there is no copy 
from immutable to immutable.


Very interesting I did some testing and you are right. So better 
to stick with .dup!


Thanks for the info,

Matheus.



Re: dChar Error

2022-12-30 Thread Ali Çehreli via Digitalmars-d-learn

On 12/30/22 13:54, matheus wrote:

> But yes I think it will generate a copy (mutable) based on this test:

In this case it does copy but in the case of dchar[] to dchar[], there 
will be no copy. Similarly, there is no copy from immutable to immutable.


> the address is different

Good test. :)

> But I couldn't find if the target will be mutable, but I think it will
> be,

The target will always be the type the programmer specifies explicitly. 
(dchar[] in this case.)


Ali



Re: dChar Error

2022-12-30 Thread matheus via Digitalmars-d-learn

On Friday, 30 December 2022 at 15:28:05 UTC, Salih Dincer wrote:
... In this case, std.conv.to can be used for mutable dchars, 
right? For example, is this solution the right approach?


```d
auto toDchar(S)(inout S str) {
  import std.conv : to;
  return str.to!(dchar[]);
}

void main() {
  auto str3 = "ÜÇ ON "d;
  auto str4 = "BİR İKİ BEŞ "d.dup;
  auto str5 = "DÖRT ALTI YEDİ ".toDchar;

  //str5.fun(5);
}
```


Unfortunately I can't say because I'm not a skilled D programmer, 
I use mostly as a C on steroids.


But yes I think it will generate a copy (mutable) based on this 
test:


void main(){
import std.stdio;
import std.conv;

auto str1 = "BİR İKİ BEŞ ";
auto str2 = str1;
auto str3 = str2.to!(dchar[]);

writeln(str1, ", ", str1.ptr);
writeln(str2, ", ", str2.ptr);
writeln(str3, ", ", str3.ptr);
str3[0] = 'A';
writeln(str3, ", ", str3.ptr);

}

It prints:

BİR İKİ BEŞ , 5641226D8200
BİR İKİ BEŞ , 5641226D8200
BİR İKİ BEŞ , 7FB466EAE000
AİR İKİ BEŞ , 7FB466EAE000

So for str2 = str1 it is just a case of passing the reference, 
and both are pointing to the same address, while in the case of: 
"str3 = str2.to!(dchar[]);", the address is different, and 
accepts changing its content (str3[0] = 'A').


In the docs: https://dlang.org/phobos/std_conv.html#to

"String to string conversion works for any two string types 
having (char, wchar, dchar) character widths and any combination 
of qualifiers (mutable, const, or immutable)."


But I couldn't find if the target will be mutable, but I think it 
will be, unless explicitly otherwise with a cast I believe.


Anyway I would wait and see if someone more skilled could shed a 
light.


Matheus.


Re: Float rounding (in JSON)

2022-12-30 Thread Sergey via Digitalmars-d-learn

On Thursday, 13 October 2022 at 19:00:30 UTC, Sergey wrote:
I'm not a professional of IEEE 754, but just found this 
behavior at rounding in comparison with other languages. I 
supose it happened because in D float numbers parsed as double 
and have a full length of double while rounding. But this is 
just doesn't match with behavior in other languages.


So there is no luck with std.json for me. But when std is not the 
solution, third party libraries could help. I've tried ASDF. This 
is kind of archived library, but it works well, its documentation 
is small and clear (mir-ion really needs to improve 
documentation).


So in asdf we could just serialize the json and it will 
automatically round numbers with the same **magic** logic for 
floating as other languages do.


The only thing: some numbers which are usually double could be 
presented in JSON as integers. Automatically asdf convert them to 
double too. In case you need to process them exactly as integers 
you could use Variant!(int, double) as a type of the data. And 
provide your custom serializer/deserializer as it is proposed in 
asdf documentation example.

http://asdf.libmir.org/asdf_serialization.html#.serializeToAsdf

PS Thanks to Steven for his suggestions in Discord.



Re: dChar Error

2022-12-30 Thread Salih Dincer via Digitalmars-d-learn

On Friday, 30 December 2022 at 11:05:07 UTC, matheus wrote:

Are you sure about that?


Thank you for your answer. You contributed to the project I was 
working on. In this case, std.conv.to can be used for mutable 
dchars, right? For example, is this solution the right approach?


```d
auto toDchar(S)(inout S str) {
  import std.conv : to;
  return str.to!(dchar[]);
}

void main() {
  auto str3 = "ÜÇ ON "d;
  auto str4 = "BİR İKİ BEŞ "d.dup;
  auto str5 = "DÖRT ALTI YEDİ ".toDchar;

  //str5.fun(5);
}
```

SDB@79


Re: dChar Error

2022-12-30 Thread matheus via Digitalmars-d-learn

On Friday, 30 December 2022 at 10:03:20 UTC, Salih Dincer wrote:

On Friday, 30 December 2022 at 09:29:16 UTC, novice2 wrote:
On Friday, 30 December 2022 at 04:43:48 UTC, Salih Dincer 
wrote:

  ...
  // example one:
  char[] str1 = "cur:€_".dup;
  ...
  // example two:
  dchar[] str2 = cast(dchar[])"cur:€_"d;
  ...
SDB@79


why you use .dup it example one, but not use in example two?

dchar[] str2 = cast(dchar[])"cur:€_"d.dup;


If I do not use .dup in the 1st example and convert as 
cast(char[]), it gives an error. However, in the 2nd example 
using .dup does nothing. It's not working anyway!

...


Are you sure about that?

Because replacing this:

dchar[] str2 = cast(dchar[])"cur:€_"d;

with this:

dchar[] str2 = (cast(dchar[])"cur:€_").dup;

Worked for me:

8: [€_]
[cur:$  _]
6: [€_]
[cur$ _]

A small example of the problem:

import std.stdio;

void main(){
  dchar[] str1 = (cast(dchar[])"cur:€_").dup;
  dchar[] str2 = (cast(dchar[])"cur:€_");

  str1[0] = '1';
  //str2[0] = '1'; // this will give: Error: program killed by 
signal 11

}

Matheus.


Re: dChar Error

2022-12-30 Thread Salih Dincer via Digitalmars-d-learn

On Friday, 30 December 2022 at 09:29:16 UTC, novice2 wrote:

On Friday, 30 December 2022 at 04:43:48 UTC, Salih Dincer wrote:

  ...
  // example one:
  char[] str1 = "cur:€_".dup;
  ...
  // example two:
  dchar[] str2 = cast(dchar[])"cur:€_"d;
  ...
SDB@79


why you use .dup it example one, but not use in example two?

dchar[] str2 = cast(dchar[])"cur:€_"d.dup;


If I do not use .dup in the 1st example and convert as 
cast(char[]), it gives an error. However, in the 2nd example 
using .dup does nothing. It's not working anyway!


On Friday, 30 December 2022 at 05:46:32 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
Of course; I cannot see anything else that could cause this in 
the assembly either.


I'm not sure I understand this issue.

SDB@79



Re: dChar Error

2022-12-30 Thread novice2 via Digitalmars-d-learn

On Friday, 30 December 2022 at 04:43:48 UTC, Salih Dincer wrote:

  ...
  // example one:
  char[] str1 = "cur:€_".dup;
  ...
  // example two:
  dchar[] str2 = cast(dchar[])"cur:€_"d;
  ...
SDB@79


why you use .dup it example one, but not use in example two?

dchar[] str2 = cast(dchar[])"cur:€_"d.dup;