Friday 22 July 2011

Mutable Types Part 2: dynamic

Another implication of mutable types and unified compilation is dynamic. Here I'm referring to the specifier used in .NET 4 to refer to types where operations are determined at run-time, and the compiler will accept anything. In "DeadMG++" then we can implement it as a library using mutable types. dynamic will operate like boost::any does in C++ in terms of holding and storing any value. The new trick is how it can be operated on dynamically- that is, when you attempt to access a member or function, then a new one will be generated and added, and implemented or thrown on.

type dynamic = struct {
    type base = struct {}
    std::unique_ptr(base);
public:
    dynamic(object) {
        type derived = struct : base {
            decltype(object).remove_reference() value;
            // implement constructors & etc
        }
        base = std::dynamic_allocate(std::unique, derived, forward(object));
    }
    // etc
};
dynamic.on_change(
    event = member_variable_access,
    action = [&](name) {
        base.public_functions.insert(
            name := "__" + name, // reserved names
            function := [&](this) {
                if (decltype(this).has_public_variable(name))
                    return dynamic(this.name);
                else
                    throw std::no_operation(...);
            },
            virtual := true
        );


    }
    // Repeat for member function

);


This is, of course, somewhat simplified pseudo-code. I haven't gone anywhere near locking down the API of type. However, I hope that you can get the gist of it.

One language that can do dynamic types, static types, type inference, meta-programming? Now that's what I call generic.

No comments:

Post a Comment