www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - auto function attributes based on type

reply "amber" <amber.swan gmail.com> writes:
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
Mar 11 2015
parent reply "weaselcat" <weaselcat gmail.com> writes:
On Thursday, 12 March 2015 at 03:12:15 UTC, amber wrote:
 ...
http://dlang.org/function.html#function-attribute-inference might be a good read if you haven't read it already.
Mar 11 2015
parent reply "amber" <amber.swan gmail.com> writes:
On Thursday, 12 March 2015 at 04:04:28 UTC, weaselcat wrote:
 On Thursday, 12 March 2015 at 03:12:15 UTC, amber wrote:
 ...
http://dlang.org/function.html#function-attribute-inference might be a good read if you haven't read it already.
I did read it but didn't really understand it, so I've come to D.learn for more help. I think it means I can just write the function like so: struct S(T) { someFunc(int i) {// impl} } and if possible it will be pure, nothrow, safe and nogc. thanks, amber
Mar 11 2015
next sibling parent reply "amber" <amber.swan gmail.com> writes:
On Thursday, 12 March 2015 at 04:51:42 UTC, amber wrote:
 On Thursday, 12 March 2015 at 04:04:28 UTC, weaselcat wrote:
 On Thursday, 12 March 2015 at 03:12:15 UTC, amber wrote:
 ...
http://dlang.org/function.html#function-attribute-inference might be a good read if you haven't read it already.
I did read it but didn't really understand it, so I've come to D.learn for more help. I think it means I can just write the function like so: struct S(T) { someFunc(int i) {// impl} } and if possible it will be pure, nothrow, safe and nogc. thanks, amber
This works nicely after checking with __traits(getFunctionAttributes, S!T.someFunc) Very cool :) bye, amber
Mar 11 2015
parent "weaselcat" <weaselcat gmail.com> writes:
On Thursday, 12 March 2015 at 05:01:50 UTC, amber wrote:
 On Thursday, 12 March 2015 at 04:51:42 UTC, amber wrote:
 On Thursday, 12 March 2015 at 04:04:28 UTC, weaselcat wrote:
 On Thursday, 12 March 2015 at 03:12:15 UTC, amber wrote:
 ...
http://dlang.org/function.html#function-attribute-inference might be a good read if you haven't read it already.
I did read it but didn't really understand it, so I've come to D.learn for more help. I think it means I can just write the function like so: struct S(T) { someFunc(int i) {// impl} } and if possible it will be pure, nothrow, safe and nogc. thanks, amber
This works nicely after checking with __traits(getFunctionAttributes, S!T.someFunc) Very cool :) bye, amber
You can also use the __PRETTY_FUNCTION__ keyword to display information about a function. http://dlang.org/traits.html#specialkeywords
Mar 11 2015
prev sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Thu, 12 Mar 2015 04:51:40 +0000, amber wrote:

 On Thursday, 12 March 2015 at 04:04:28 UTC, weaselcat wrote:
 On Thursday, 12 March 2015 at 03:12:15 UTC, amber wrote:
 ...
http://dlang.org/function.html#function-attribute-inference might be a good read if you haven't read it already.
=20 I did read it but didn't really understand it, so I've come to D.learn for more help. =20 I think it means I can just write the function like so: =20 struct S(T) { someFunc(int i) {// impl} } =20 and if possible it will be pure, nothrow, safe and nogc.
yes. all templated functions (and `someFunc()` is templated due to `S`=20 being templated) are subjects of attribute inference. due to this fact=20 people sometimes writing even free functions as argument-less templates, void freeFunc() (...) { ... } so compiler will infer attributes for `freeFunc()`. this has almost no=20 cost, as compiler will merge all produced templates into one.=
Mar 12 2015