On Wed, 19 Nov 2014 12:33:17 +0000
via Digitalmars-d <[email protected]> wrote:

> D doesn't allow construction from a different type with the `Type 
> name = value;` syntax. Try this:
har-har... here comes the power of the dark side!

  struct S {
    bool filler; // just in case, to avoid S("str")
    string s;

    @disable this (); // just in case too
    this (string v) { s = "s|"~v; }
    this (int v) { import std.conv : to; s = "i|"~to!string(v); }
    this (double v) { import std.conv : to; s = "d|"~to!string(v); }

    string toString () const nothrow @nogc { return s; }
  }


  void main () {
    import std.stdio;
    S s0 = "string";
    S s1 = 42;
    S s2 = 666.0;
    writeln(s0); // writes "s|string"
    writeln(s1); // writes "i|42"
    writeln(s2); // writes "d|666"
  }

and remember, we have cookies.

Attachment: signature.asc
Description: PGP signature

Reply via email to