Thread overview | |||||
---|---|---|---|---|---|
|
January 24, 2013 An default values for generics | ||||
---|---|---|---|---|
| ||||
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 Re: An default values for generics | ||||
---|---|---|---|---|
| ||||
Posted in reply to goodwin | I think return T.init; will do what you want most easily. |
January 24, 2013 Re: An default values for generics | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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.
|
Copyright © 1999-2021 by the D Language Foundation