Inheritance of purity

Walter Bright newshound2 at digitalmars.com
Thu Feb 16 20:53:30 PST 2012


On 2/16/2012 7:54 PM, Jonathan M Davis wrote:
> On Thursday, February 16, 2012 19:41:00 Walter Bright wrote:
>> On 2/16/2012 7:23 PM, Jonathan M Davis wrote:
>>> No. Absolutely not. I hate the fact that C++ does this with virtual. It
>>> makes it so that you have to constantly look at the base classes to
>>> figure out what's virtual and what isn't. It harms maintenance and code
>>> understandability. And now you want to do that with @safe, pure, nothrow,
>>> and const? Yuck.
>> I do not see how it harms maintainability. It does not break any existing
>> code. It makes it easier to convert a function hierarchy to nothrow, pure,
>> etc.
>
> It makes it harder to maintain the code using the derived classes, because you
> end up with a bunch of functions which aren't labeled with their attributes.
> You have to go and find all of the base classes and look at them to find which
> attributes are on their functions to know what the attributes of the functions
> of the derived classes actually are. It will make using all D classes harder.

I doubt one would ever need to dig through to see what the attributes are, because:

1. The user of the override will be using it via the base class function.

2. The compiler will tell you if it, for example, violates purity. There won't 
be any guesswork involved. Right now, the compiler will give you a covariant error.

3. It isn't different in concept than auto declarations and all the other type 
inference that goes in D, including automatic inference of purity and safety.


> You should be able to look at a function and know whether it's pure, @safe,
> nothrow, or const without having to dig through documentation and/or code
> elsewhere to figure it out.
>
> Doing this would make the conversion to const easier but be harmful in the
> long run.

We should be encouraging people to use pure, @safe, etc. Not doing the inference 
makes it annoying to use, and so people don't bother. My experience poking 
through the druntime and phobos codebase is that the overwhelming majority of 
the functions are safe, const & pure, but they aren't marked that way.



More information about the Digitalmars-d mailing list