DIP64: Attribute Cleanup

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon Apr 18 20:24:47 PDT 2016


On Monday, April 18, 2016 20:49:45 jmh530 via Digitalmars-d wrote:
> As an aside, the reason for not having function attribute
> inference everywhere is that it doesn't work when function bodies
> are not available. How common is this? I feel like I never write
> functions like this. Why not just make the rule that if the
> function body is available, then inference will happen?

There is no guarantee that the source code will be available, so the
attributes cannot be infered in the general case. In addition, if you don't
make the attributes explicit, you have the problem that you have no idea
which attributes apply to a function, which makes managing them harder and
on some level defeats the purpose of having them. Another major problem is
that if the attributes aren't explicit, then it's far easier to accidentally
break code by accidentally changing the attributes. We pretty much had to
add attribute inference to the language to make attributes and templates
work together (since which attributes will work is often dependent on the
template arguments), but normal functions don't need that inference.

I would strongly argue that it's bad practice to rely on attribute inference
when it's not required - even with templated functions. I'd even go so far
as to argue that using labels like

@safe:

or blocks like

@safe
{
}

is generally bad practice, because it makes it harder to figure out which
attributes apply to a given function, and while it might be nice to save on
typing those extra characters for the attributes, you're causing more work
for anyone who has to maintain those functions later - including yourself -
because it's not immediately clear which attributes apply.

- Jonathan M Davis


More information about the Digitalmars-d mailing list