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
  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 • • • • •
  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) • • • • •
  4. 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 • • •
  5. 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) • • • • •
  6. Meta-programming at Runtime self-modifying code like JIT indirect control flow

    via state or closures text processing: Jinja2, Mustache Lisp & Scheme runtime macros • • • •
  7. 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) • • • •
  8. 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 • • • • • •
  9. Impressions Meta-Objects Is that kind of like the little meeting

    that sets the agenda for the big meeting?
  10. 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. • • • •
  11. 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!) • • • • •
  12. 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) • • • •
  13. 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? • • • •
  14. 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 • • • •
  15. 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. • • •
  16. 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 • • • •
  17. 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 • • • •
  18. 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 • • • • • •
  19. 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 • • • • • •
  20. 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) • • • • • •
  21. 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 • • • • •
  22. 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 • • • • •
  23. 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 • • • • •
  24. 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” • • • •
  25. 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 • • • • •
  26. 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. • • •
  27. 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. • • • •