1. 7

You can abstract without giving names: The Design by Introspection technique

  1.  

  2. 3

    This seems like a hot topic, only the other day were we discussing AST editors, Intentional Programming and AOP.

    I’d never heard the term “Design by Introspection” before, and that certainly may be a term that Andrei coined, but he certainly didn’t invent this, and he certainly didn’t invent static if, although he is owed much gratitude for taking it from its initial inception to the beautiful implementation in DLang. Nice to see C++ finally catching up btw, which is ironic because AFAIK Visual C++ 6.1 was the first compiler from a mainstream vendor to feature both static if and Design by Introspection, (well that and AspectJ).

    As much as I love the meta-programming facilities in DLang, there is a difference to how Andrei approached it and a less tightly bound approach. In DLang, it’s hard for Design by Introspection to be done by someone other than the same programmer who wrote the regular code. That is to say there is a cross-cutting concern.

    Another approach is that the allocator is chosen by external forces - enzymes that do deep introspection of the local or global AST.

    What you do is open up the API to the compiler at the point just after the AST has been built and instead of hard-coding optimizer algorithms, you provide an open plug-in model where your “Intentional Programming” enzymes go and get busy on the code. These enzymes can be totally hidden and autonomous, or they can be exposed as optionally parameterized meta-tags that allow the application programmer to tweak the settings to guide the intentions.

    In this way, things like optimizers are just a small class of things that can be written. You can just as easily have a rule that looks at your code in the middle of the compilation and sends a mail to your boss if you forgot to use Hungarian notation.

    Now you have that, selecting allocators is left to people who know about selecting allocators, and you get to write

    std::vector<SomeType> myBigFatVector;
    

    or if you want to be explicit

    [ fix_my_allocator ]
    std::vector<SomeType> myBigFatVector;
    

    or

    [ meta::allocator::hint(meta::allocator::type::slab) ]
    std::vector<SomeType> myBigFatVector;
    

    … knowing that someone else, who knows a lot more about allocation strategies and the big picture has built something smart that does the right thing. these are examples not real code

    More importantly, the policy that goes with the enzyme is not embedded in the code that rides with the implementation of either vector or a specific allocator, and that policy can be changed at the drop of a hat. Bye-bye cross-cutting concerns.

    Anyway, there is a lot more to it than this, and I’ve barely touched on it here, but yes, “Design by Introspection” is extremely powerful and opens many doors where you only need provide your intention.

    We had it to the point where I could mark up a class like so:

    [ Window ]
    class MyWindow
    {
        onMouseDown(auto mouseEvent) { ... }
    };
    

    That thing would add you window creation code, message handler code, wire up the message handlers, tell the linker to add additional resources and a whole lot more, working not only inside the language, but across the entire tool-chain.

    We even had Bjarne and a bunch of the committee bought in, but politics and time constraints led to a less than adequate solution, that’s not to say it still isn’t worth pushing for, and honestly this technique has nothing to do with C++ or any particular language of course.

    Now, back to the title “Abstracting is NOT about names”, well abstracting is about elevating the communication and implementation of concepts (i’m not talking C++ concepts here). The names of the abstractions are important, and so is the implementation of how the concepts get manifested, and to me they kind of go hand in hand.

    That said, concepts are generally not their own implementors, but for implementations to come-a-running, it’s necessary to fully convey the idea and that means the aspects of their identity.. to which you can give a name.

    Sorry for the long post - just my 2c

    I like the article by the way, and I’m very happy to see this stuff being explored.