GTKD2 GValue Type

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 19 05:31:04 PST 2014


On Wed, 19 Nov 2014 12:33:17 +0000
via Digitalmars-d <digitalmars-d at puremagic.com> 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141119/06864ef6/attachment-0001.sig>


More information about the Digitalmars-d mailing list