1 /**
2    A fictional language implemented in D
3  */
4 module blub;
5 
6 
7 import std.traits: Unqual;
8 
9 
10 struct Blub {
11     enum Kind {
12         integer,
13         string,
14     }
15 
16     Kind kind;
17     // normally we'd use a union but meh about storage here
18     private int _integer;
19     private string _string;
20 
21     @disable this();
22 
23     this(int i) @safe @nogc pure nothrow {
24         kind = Kind.integer;
25         _integer = i;
26     }
27 
28     this(string s) @safe @nogc pure nothrow {
29         kind = Kind..string;
30         _string = s;
31     }
32 
33     int asInteger() @safe @nogc pure const {
34         if(kind != Kind.integer) throw new Exception("not an int");
35         return _integer;
36     }
37 
38     string asString() @safe @nogc pure const {
39         if(kind != Kind..string) throw new Exception("not a string");
40         return _string;
41     }
42 }
43 
44 
45 Blub toBlub(int i) {
46     return Blub(i);
47 }
48 
49 Blub toBlub(string s) {
50     return Blub(s);
51 }
52 
53 T to(T)(Blub blub) if(is(Unqual!T == int)) {
54     return blub.asInteger;
55 }
56 
57 T to(T)(Blub blub) if(is(Unqual!T == string)) {
58     return blub.asString;
59 }