Templates: generic "return null;"

Marco Leise Marco.Leise at gmx.de
Thu Feb 6 19:48:08 PST 2014


Am Mon, 03 Feb 2014 10:25:17 +0000
schrieb "Chris" <wendlec at tcd.ie>:

> MyStruct(T) {
>    T[T] attributes;
>    // ....
>    public auto getAttribute(T attr) {
>        if (!(attr in attributes)) {
>          return null; // Doesn't work for numbers!
>        }
>        return attributes[attr];
>      }
> }
> 
> void main() {
>    auto myStr = MyStruct!int(0); // Error
> }

MyStruct(T) {
   T[T] attributes;
   // ....
   public auto getAttribute(T attr) {
       return attr in attributes;
     }
}

There you go.

-- 
Marco



More information about the Digitalmars-d-learn mailing list