"default" keyword as function attribute
Jakob Ovrum via Digitalmars-d
digitalmars-d at puremagic.com
Sat Mar 19 07:55:00 PDT 2016
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.
A recent example is this Phobos PR[1] which cannot apply `@safe`
module-wide because the module contains generic functions where
attribute inference is desirable. In this particular case it
could be avoided by defining those generic functions before
declaring `@safe:`, but this is at best unintuitive organization
of code, and at worst highly intrusive when the generic function
is a member function as it affects the whole type. It would also
affect generated documentation with the current state of our
tools.
DIP 79[2] suggests a number of possible language amendments that
would help with this problem. The @attribute(<boolean
expression>) syntax has support from (and was originally proposed
by?) Andrei, but it doesn't really help with the attribute
inference problem so I think we should consider it a separate
issue.
It suggests "default" as a possible function attribute which
negates previously applied attributes, which seems like it would
solve the problem with minimal change to the language. Maybe
there is a better way, or maybe we should move forward with it.
What do you think?
Fixing this problem would encourage attribute proliferation while
facilitating correct use of attribute inference.
[1] https://github.com/D-Programming-Language/phobos/pull/4082
[2] http://wiki.dlang.org/DIP79
More information about the Digitalmars-d
mailing list