Inheritance of purity

Steven Schveighoffer schveiguy at yahoo.com
Fri Feb 17 12:38:51 PST 2012


On Thu, 16 Feb 2012 21:49:40 -0500, Walter Bright  
<newshound2 at digitalmars.com> wrote:

> Given:
>
>      class A { void foo() { } }
>      class B : A { override pure void foo() { } }
>
> This works great, because B.foo is covariant with A.foo, meaning it can  
> "tighten", or place more restrictions, on foo. But:
>
>      class A { pure void foo() { } }
>      class B : A { override void foo() { } }
>
> fails, because B.foo tries to loosen the requirements, and so is not  
> covariant.
>
> Where this gets annoying is when the qualifiers on the base class  
> function have to be repeated on all its overrides. I ran headlong into  
> this when experimenting with making the member functions of class Object  
> pure.
>
> So it occurred to me that an overriding function could *inherit* the  
> qualifiers from the overridden function. The qualifiers of the  
> overriding function would be the "tightest" of its explicit qualifiers  
> and its overridden function qualifiers. It turns out that most functions  
> are naturally pure, so this greatly eases things and eliminates annoying  
> typing.
>
> I want do to this for @safe, pure, nothrow, and even const.
>
> I think it is semantically sound, as well. The overriding function body  
> will be semantically checked against this tightest set of qualifiers.
>
> What do you think?

I think it feels like a (welcome) about face from previous stances.  But I  
share some concern with others in that we are separating the attributes of  
a function from its declaration to the point where we need a tool to  
determine what the exact attributes of a function are.

I shudder to think about how I would understand some of the CSS I have to  
deal in my daily work if I didn't have firebug.  I hope D doesn't become  
similar.

Of course, if we get to the point where DDoc is full-featured, one will  
almost never have to look at function declarations when using them.

Also, I like how you *can* repeat the attributes if it's necessary to.

Could there be a compiler option for requiring overriding functions to  
repeat base attributes?  Or at least print out where they are while  
compiling?  At least then you can see where your attributes aren't  
repeated.

-Steve


More information about the Digitalmars-d mailing list