Walter's second axiom

Janice Caron caron800 at googlemail.com
Sat Dec 8 04:49:48 PST 2007


As discussed elsewhere, Walter's second axiom states: for any type T,
it should be possible to construct a struct S which behaves exactly
like a T, by wrapping T inside a struct and adding extra member
functions, like so:

    struct S
    {
        T t;
        /* functions */
    }

However, there is at least one violation of the axiom: inheritance. As follows:

    class C {}
    struct S { C c; }

I can do

    class D : C {}

but I can't do

    class D : S {}

thus, S cannot be made to behave exactly like C, and so the axiom does not hold.

The solution is to provide structs with some mechanism that allows
classes to inherit from them - such as, for example, opInherit()

    struct S
    {
        C c;
        C opInherit() { return c; }
    }

A class could then inherit from either a class, or from a struct which
supplies opInherit(). opInherit() itself must, of course, return
either a class, or a struct which supplies opInherit().

This is not a feature request as such. I'm just mentioning - from a
purely theoretical standpoint - a violation of Walter's second axiom,
and a possible remedy.

(And just to stir things up further, I think that interfaces violate
the second axiom also) :-)



More information about the Digitalmars-d mailing list