Menu
Home
Create new Paste
Log in
Code
Theme: cobalt
Theme: eclipse
Theme: elegant
Theme: monokai
Theme: neat
Theme: night
Theme: rubyblue
import std.stdio; struct Num { private: union Num_ { float fvalue; int ivalue; } final enum Type { None, Float, Int } Num_ _num; Type _type; //bool _assigned; private: void _assign(T)(T val) { static if (is(T : int)) { writeln("foo : ", val); this._type = Type.Int; this._num.ivalue = val; } else if (is(T : float)) { writeln("bar : ", val); this._type = Type.Float; this._num.fvalue = val; } else { throw new Exception("Unefined number type."); } } public: this(T)(T val) { this._assign(val); } typeof(this) opAssign(T)(T val) { this._assign(val); return this; } @property auto get() const { write(" -> ", this._type, " : "); switch (this._type) { case Type.Int: write("ret: ", this._num.ivalue, " <-> "); return this._num.ivalue; case Type.Float: return this._num.fvalue; default: writeln("default"); return 0; } } alias get this; } void main() { Num zahl; zahl = 3.14; writeln("zahl is ", zahl); zahl = 42; writeln("zahl is now ", zahl); }
Result:
Success
/
Return code: 0
/
Compilation time:
0.386
seconds
/
Run time:
0.001
seconds
Application output:
bar : 3.14
zahl is -> Float : 3.14
foo : 42
zahl is now -> Int : ret: 42 <-> 0
Username
Message
Add comment
Paste info
Author:
Namespace
Views:
172
Private:
no
Expires:
Never
Uploaded:
11.09.12 19:01
Votes
:
0
Tweet
Compilation
Compiler:
DMD 2.060
Pointer size:
m64
Actions
Download
Fork
Raw
×
Confirm
Are you sure you want to delete this paste?
There's no way back!
×
Confirm
Reason