Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Meta: Programming, Objects, and Classes

Meta: Programming, Objects, and Classes

Demystifying meta programming and acknowledging it as something we do on a regular basis. The big question is the degree of abstraction.

Shane Holloway

April 25, 2012
Tweet

More Decks by Shane Holloway

Other Decks in Programming

Transcript

  1. Meta: Programming,
    Objects, and Classes
    presented by Shane Holloway
    of TechGame Networks
    ( [email protected] )
    share under Creative Commons BY-NC-SA 3.0

    View Slide

  2. Who is this guy?
    CEO of TechGame Networks (techgame.net)
    B.S. Computer Science from UCCS
    Started software career in 1996
    Passion for dynamic systems design & impl.
    Software as a process of creative synthesis
    combining art and science





    View Slide

  3. Background Experience
    Instrumentation, Hardware Automation,
    Scientific Visualization, Accessibility
    10 years Python
    10 years C++ (some Objective-C & ANSI C)
    projects in: Javascript, Smalltalk, Lua,
    OMeta, Lisp, Scheme, Coffee-script
    pre-career: Pascal & Object Pascal (Delphi)





    View Slide

  4. Let’s get Meta
    “Fingerpainting. Escher revisited”, by Joseandrés Guijarro
    used under CC BY-NC 2.0 from http://bit.ly/HX8QnV

    View Slide

  5. Impressions
    Meta
    Blurt out the ideas it brings to mind!

    View Slide

  6. Definition: meta
    adjective:
    (of a creative work) referring to itself or to
    the conventions of its genre; self-referential.
    combining form:
    denoting position behind, after, or beyond
    denoting something of a higher or second-order
    kind



    View Slide

  7. Impressions
    Meta-
    Programming
    Don’t we have enough “real” programming?

    View Slide

  8. Meta-programming
    concept: “does-a” expansion
    C preprocessor macros
    compile-time optimizations: loop unrolling
    C++ Templates and Generics
    polymorphism, operators, and protocols
    (leverages generics by redefining methods)





    View Slide

  9. Meta-programming
    at Runtime
    self-modifying code like JIT
    indirect control flow via state or closures
    text processing: Jinja2, Mustache
    Lisp & Scheme runtime macros




    View Slide

  10. Languages are
    Meta-Programing
    Both C++ (Cfront) and Objective-C started
    as transpilers targeting C
    Alt-JS: Coffee-script & Dart to Javascript
    Domain-Specific Languages
    Remember T-diagrams for bootstrapping
    compilers? (http://en.wikipedia.org/wiki/T-diagram)




    View Slide

  11. May Already Use Meta
    CMake: CMake generates native makefiles and workspaces
    that can be used in the compiler environment of your choice.
    SWIG: “Simplified Wrapper and Interface
    Generator”
    Parser-Generators: Lex, Yacc, Flex, Bison
    Templates for Eclipse, XCode, Visual Studio
    Protocol Buffers, Corba, ActiveX, IDL
    Markdown, Textile, Haml, Jade, Less, Sass, Stylus






    View Slide

  12. Impressions
    Meta-Objects
    Is that kind of like the little meeting that sets
    the agenda for the big meeting?

    View Slide

  13. Meta-object
    concept: “has-a” compositional
    from wikipedia: any entity that manipulates,
    creates, describes, or implements other objects.
    related: reflection, (structural) recursion,
    meta-object protocol
    in languages like C++ and Java, the meta-
    object protocol is defined by the language,
    while in Objective-C it can be tailored.




    View Slide

  14. Meta-object Powered
    Object-oriented: Smalltalk, CLOS, Python
    Prototype-oriented: self, javascript, lua
    Aspect-oriented
    Traits, Objective-C Categories
    Lieberman prototypes (ask me later, they are cool!)





    View Slide

  15. Impressions
    Meta-Classes
    Just be happy that we gave you class definitions
    and inheritance!

    View Slide

  16. Meta-classes
    concept: “is-a” behavior through identity
    subcategory of Meta-objects, particularly
    Meta-object protocol in the context of
    Python, Lua, Lisp, Smalltalk, Cola
    object-oriented programming
    prototype-oriented programming
    (under a generalized understanding)




    View Slide

  17. Structural Recursion in
    “Pure” OO Languages
    Everything is an object.
    All objects have a class.
    An object’s class must also be an object.
    As all objects have a class, and a class is an
    object, what class is the class of a class?




    View Slide

  18. Meta-class: a Step Back
    A meta-class creates classes when
    provoked
    A class creates instances of objects
    An object combines state and behavior
    An instance is an object that has a class




    View Slide

  19. Why Meta?
    The bedrock of computing is built upon two
    concepts: data and the next instruction.
    Meta-* disciplines are the art of
    abstracting these fundamentals to models
    that suit the way we want to think about
    computing.
    Goal: improve tradeoffs [cost, time, quality]
    by investing in abstraction to leverage
    features, productivity, and uniformity.



    View Slide

  20. Why Meta-*?
    Allows you to use the computer to work
    smarter instead of harder.
    e.g. the same reason you use a computer
    language over assembly
    When you want to think about the problem
    space from a higher level abstraction
    similar to abstraction to a map/reduce
    operation from an explicit loop




    View Slide

  21. When to Meta?
    When leverage from developing and
    employing solution will yield >2x over the
    lifetime cost of manual process.
    When the meta tools are already built
    Templates, macros, factories, extension
    Experience comes from practice




    View Slide

  22. Time Continuum
    People: idea, funding, design, coding, support
    Time Scale: Days to years
    Software: compile, link, initialize, runtime, hot path.
    Time Scale: Milliseconds to hours
    Hardware: fetch, execute, store, branch
    Time Scale: Picoseconds to seconds






    View Slide

  23. How to Meta?
    (on purpose)
    Reflection! getattr(self,‘on_’+name)(evt)
    Web: url path, args, mimetype, accept
    Events, observers, broadcast, msg queues
    Plugins & runtime composed systems
    metaclass registers component
    reflection + delegation for plugin hooks






    View Slide

  24. Dynamic Dispatch
    (Meta-object protocol)
    Runtime resolution of code or object impl.
    by key, arg types, values, or internal
    state
    commonly stored in associative mappings
    Also known as message sending
    Smalltalk, Objective-C, RPC
    Actors & Coroutines (Erlang, IO, Scala)






    View Slide

  25. Javascript Browser
    Events
    Add context to DOM elements
    data-verb, id, class, ...
    Capture all interesting events
    grab event target’s context
    resolve action using context & event type





    View Slide

  26. Object Composition
    (GoF Design Patterns)
    “Design Patterns: Elements of Reusable Object-Oriented Software”, Gamma et al. © 1995
    Commands with State & Strategy patterns
    Context dependent actions
    Undo/redo stacks, automation
    Inversion of Control, Builder pattern
    Proxy, Composite and Decorator patterns





    View Slide

  27. Extending Python
    (from the inside)
    http://hg.techgame.net/TG/metaObserving/
    Use __metaclass__ mechanism to provide
    extension points:
    observe subclass creation
    observe instance creation
    observe instance restore (pickling)
    Just add instances to the namespace that
    support the observation protocol





    View Slide

  28. Fuzzy Boundaries
    Is it object composition or meta-*?
    Does it matter?
    Did you avoid explicit cases by your impl.?
    Higher-order thinking, be it functions,
    composition, or meta-*, can all help you
    avoid “the giant switch statement”




    View Slide

  29. To Meta or Not
    solving indirect problem
    fun rabbit hole: challenging puzzles!
    creation: accidental or intentional?
    audience: creation vs maintenance
    tech. debt: troubleshooting and evolving





    View Slide

  30. Difficulties with Meta
    Debugging and tracing. Effects can split
    between compile-time expansion,
    initialization, runtime and structural
    composition.
    Non-explicit control flow can lead to
    “magical” feeling of action-at-a-distance
    Can be similar to tracing through async
    event driven or queue based systems.



    View Slide

  31. Intention and Audience
    Be intentional when integrating processed
    output into your software system.
    Keep it simple to explain and review
    Implement test suites and write
    documentation that anticipate questions and
    likely changes.
    Keep the people who will be maintaining the
    system in mind.




    View Slide

  32. Let’s get Meta
    “Fingerpainting. Escher revisited”, by Joseandrés Guijarro
    used under CC BY-NC 2.0 from http://bit.ly/HX8QnV

    View Slide