"default" keyword as function attribute
Jonathan M Davis via Digitalmars-d
digitalmars-d at puremagic.com
Mon Mar 21 15:58:35 PDT 2016
On Saturday, 19 March 2016 at 14:55:00 UTC, Jakob Ovrum wrote:
> We often repeat advice to put `@safe:`, or some other function
> attribute, at the top of your modules to apply the attribute en
> masse.
>
> In practice this quickly becomes infeasible. Sooner or later, a
> generic function is introduced to idiomatic D modules, and many
> of them, perhaps most of them, are more general when attribute
> inference is used. Any explicit attribute application disables
> inference of that attribute. In my experience this is by far
> the most common reason that blanket application of attributes
> is impractical.
I'm not necessarily against an addition to the language that
would make it easier to reset the attributes to their "defaults"
when attribute: or attribute{} has been used, but I confess that
in general, I'm inclined to think that folks should think twice
before apply attributes en masse like that. It makes it _way_ too
easy to miss that the attribute is being applied when it's
somewhere else entirely in the file. As ugly as it is, in
general, I think that it makes far more sense to be explicit
about it and put @safe, nothrow, pure, etc. directly on the
functions and not use : or {}.
Personally, I only ever do it with access levels, which should
generally be grouped together anyway IMHO, and even then,
sometimes it's confusing about whether something is public or
private without digging around elsewhere in the file to figure
out whether a particular section is public or private. With
@safe, pure, nothrow, etc., it's even worse, because it doesn't
usually make sense to group functions based on those attributes,
and you often do have to change one of those attributes on a
particular function. It also makes it easy to accidentally apply
attributes to templated functions, which is obviously bad.
So, I'd honestly be inclined to argue that doing stuff like
@safe: or pure: is bad practice.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list