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 E... Salih Dincer via Digitalmars-d-learn
      • Re: dCh... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
  • Re: dChar Error novice2 via Digitalmars-d-learn
    • Re: dChar E... Salih Dincer via Digitalmars-d-learn
      • Re: dCh... matheus via Digitalmars-d-learn
        • Re:... Salih Dincer via Digitalmars-d-learn
          • ... matheus via Digitalmars-d-learn
            • ... Ali Çehreli via Digitalmars-d-learn
              • ... matheus via Digitalmars-d-learn
              • ... Salih Dincer via Digitalmars-d-learn
              • ... Salih Dincer via Digitalmars-d-learn
              • ... Ali Çehreli via Digitalmars-d-learn
              • ... Salih Dincer via Digitalmars-d-learn

Reply via email to