www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Type aliasing

reply "js.mdnq" <js_adddot+mdng gmail.com> writes:
I have a static class that generates unique ID's. The id's are 
hardcoded as ints.

I would like to make this extensible and allow for other integer 
numeric values to be(byte, ulong, etc...).

I can simply make the static class generic without issues.

The problem is the return type of the Get ID function, while 
generic, requires hardcoding the saved ID in a class.

e.g.,

class myclass {
    int ID;
}

while I want ID's type to be that of the uniqueID's type.

e.g.,

class myclass {
    auto ID = uniqueID!T.Get();
}

would make ID a type T (T is defined by the user and known at 
compile time). In another class one might use a different type.

Is anything like this possible? It is purely for convenience as 
I'm trying to avoid having to change the type at two or more 
places.
Dec 04 2012
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Tuesday, 4 December 2012 at 16:34:50 UTC, js.mdnq wrote:
 class myclass {
    int ID;
 }
Try something like this: class myclass { typeof(uniqueID!T.Get()) ID; this() { ID = uniqueID!T.Get(); } } The typeof(expr...) can be used anywhere a type can be used.
Dec 04 2012
parent "js.mdnq" <js_adddot+mdng gmail.com> writes:
On Tuesday, 4 December 2012 at 16:42:21 UTC, Adam D. Ruppe wrote:
 On Tuesday, 4 December 2012 at 16:34:50 UTC, js.mdnq wrote:
 class myclass {
   int ID;
 }
Try something like this: class myclass { typeof(uniqueID!T.Get()) ID; this() { ID = uniqueID!T.Get(); } } The typeof(expr...) can be used anywhere a type can be used.
Cool, I'll try it but ran into one problem after making my class generic. Yours should work, thanks.
Dec 04 2012