Saturday, 31 May 2014

Fun with inheritance!

Along the design process of Wide, many people have suggested preprocessing to C++. I declined to go that route because I wanted to have the power to offer different semantics. A simple example is strict left-to-right evaluation order. Another is defined overflow/underflow on integers. But here's a more complex example: virtual functions. In C++, you can have

    class base {
        virtual base* clone();
    }
    class derived : base {
        virtual derived* clone();
    }

This is great, but not safe- we really need to use smart pointers. Unfortunately, this leads to an unfortunate surprise.

    class base {
        virtual std::shared_ptr<base> clone();
    }
    class derived : base {
        virtual std::shared_ptr<derived> clone(); // error
    }

This is obviously bad. So in Wide, I've decided for virtual function arguments and returns, we will use is-a- it's like implicitly convertible, but I've decided for it to have a deeper meaning. This means that the above sample, when converted to Wide, is legal.

The problem is when you want to express this Wide hierarchy as C++- because the converted code is illegal. Strictly from a vtable point of view, if I export the constructor then I can set whatever vtable I want in it. But, for example, if the base class function is pure virtual, I need Clang to recognize that derived is not abstract. There is a method in Clang that suggests this but who knows if it can handle the idea of one method overriding another even when the Standard says it should not.

 So, yes. Now you can inherit from a C++ interface, but what's going to happen if you try to convert that type to a C++ type, I don't know.

No comments:

Post a Comment