www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - An default values for generics

reply "goodwin" <artem.3a gmail.com> writes:
Hello everybody.

I started to study the D programming language and i write same 
small library which use templates(generics) active. So i have one 
question, how can i get default value for template(generic)? May 
be something like this:

T someMethod(T)(bool someCondition){
     if (someCondition) {
         // ... do something and return result
     } else {
         return default(T);
     }
}

I solved this problem with help small hack:

T getDefaultValue(T)(){
     T default_value;
     return default_value;
}

But i don't like this solution and i don't sure that it is work 
in all cases. I want to use standard mechanism if it is exist.

With best regards.

PS: Sorry for my bad english. =)
Jan 23 2013
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
I think

   return T.init;

will do what you want most easily.
Jan 23 2013
parent "goodwin" <artem.3a gmail.com> writes:
Thanks, this is what i need.

On Thursday, 24 January 2013 at 02:11:37 UTC, Adam D. Ruppe wrote:
 I think

   return T.init;

 will do what you want most easily.
Jan 23 2013