Attribute inference for non-templated functions

Mathias Lang via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 30 05:57:56 PDT 2016


On Wednesday, 30 March 2016 at 12:42:42 UTC, Seb wrote:
> Hey while I was writing the contribution guide I stumbled over 
> the issue that dmd will automatically infer attributes like 
> @safe or pure in templated functions, but not in non-templated 
> ones. Consider this example:
>
> ```
> size_t inc(size_t a)
> {
>     return a + 1;
> }
>
> pure unittest
> {
>     assert(1.inc == 2);
> }
> ```
>
> It will not compile and yield and and error like "pure function 
> 'foo.__unittestL7_1' cannot call impure function 'foo.inc'".
>
> Whereas the following compiles:
>
> ```
> size_t incT(T = size_t)(size_t a)
> {
>     return a + 1;
> }
>
> pure unittest
> {
>     assert(1.incT == 2);
> }
> ```
>
> My question is whether this is just an open issue (I couldn't 
> find it) or a design decision?

It's a design decision. You want to be able to fix the exact type 
of your function, in order to provide headers for them for 
example (so you can work with libraries for which the source code 
is not available).

If you want attribute inference, you can either make it a dummy 
template or, with a recent enough compiler, use `auto` return 
type.


More information about the Digitalmars-d mailing list