Thread overview
An default values for generics
Jan 24, 2013
goodwin
Jan 24, 2013
Adam D. Ruppe
Jan 24, 2013
goodwin
January 24, 2013
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. =)
January 24, 2013
I think

  return T.init;

will do what you want most easily.
January 24, 2013
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.