www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Default template type for class?

reply "js.mdnq" <js_adddot+mdng gmail.com> writes:
I have a class

static class myclass(T)
{
    static void myfunc();
}

which I have to call like myclass!Q.myfunc();

How can I create a default type so that I can call it like 
myclass.myfunc()?

I've tried

static class myclass(T = int) and (alias T = int), neither worked.
Dec 04 2012
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
 How can I create a default type so that I can call it like 
 myclass.myfunc()?
Can't do that. Best you can do is myclass!().myfunc, then it will use the default type (write it is (T = int) bw) but you have to put in the !() to actually use the template. There's a patch for the compiler to change this, but the compiler devs are split on if it is actually a good idea or not, because of a few cases, one I remember is: template foo(T = int) {} alias foo bar; // is bar == foo or is bar == foo!int ? bar!float; // legal or illegal? It is legal today, but with the patch it becomes questionable
Dec 04 2012
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
BTW you can do:

class myclass_custom(T = int) {}

alias myclass_custom!int myclass;


Then use myclass.whatever and you get int. If you want a custom 
type then, you'd use the other name (myclass_custom!ubyte for 
instance)
Dec 04 2012