GTKD2 GValue Type

via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 19 05:44:09 PST 2014


On Wednesday, 19 November 2014 at 13:31:16 UTC, ketmar via 
Digitalmars-d wrote:
> 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.

Ah, right, it _does_ work with explicitly defined constructors. 
Maybe GValue doesn't have those?


More information about the Digitalmars-d mailing list