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...

Reply via email to