auto function attributes based on type

amber via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 11 20:12:14 PDT 2015


Hi,

Is there a way in D to specify function attributes for a template 
struct/class by type?

E.g. given  a template struct/class as follows:

---
struct S(T) {
     T[] values;
     void someFunc(int i) pure nothrow @safe {}
}
---

For some types the pure, nothrow, @safe attributes are valid and 
for others they're not.

I'd rather not duplicate each function body just to get 
attributes working.

Hmm, just had a thought, would this work??
---

struct S(T) {
     T[] values;
     private void someFuncImpl()(int i) {
          // the impl goes here with auto-deduced attributes
     }
     // Here's the public interface
     static if(is(T == SOME_TYPE)) {
         void someFunc(int i) pure nothrow @safe 
{this.someFuncImpl(i);}
     } else {
         void someFunc(int i) {this.someFuncImpl(i);}
     }
}
---

  ...  and is it "good" D?

If it works it's still annoying, but saves duplicating the impl.


thanks,
amber


More information about the Digitalmars-d-learn mailing list