auto function attributes based on type

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 12 03:44:26 PDT 2015


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.
> 
> 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.

yes. all templated functions (and `someFunc()` is templated due to `S` 
being templated) are subjects of attribute inference. due to this fact 
people sometimes writing even free functions as argument-less templates,

  void freeFunc() (...) { ... }

so compiler will infer attributes for `freeFunc()`. this has almost no 
cost, as compiler will merge all produced templates into one.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150312/da4cc6b8/attachment.sig>


More information about the Digitalmars-d-learn mailing list