关于MetaProgramming

MOP

    These dynamic behaviors are governed by the metaobject protocol ( MOP)  that  each  of  these  languages  implements  in  their  runtime.  The metaobject protocol of a language defines the semantics of the extensibility of the language artifacts. Take a look at the accompanying callout for a gentle introduction to the concept of  MOP in programming languages.

    DEFINITION  A meta-object is an abstraction that manipulates the behavior  of other objects. In an  OOP language, a metaclass might be responsible for creating and manipulating classes. To do that, the metaclass needs to store all  information  that's  relevant  to  the  class,  like  type,  interface,  methods, and extension objects.

    A  meta-object  protocol  ( MOP) for a language defines the semantics of the extensibility of programs written in that language. The behavior of theprogram  is  determined  by  the  MOP,  including  aspects  of  the  program  that can be extended by the programmer during compile time or runtime.

    Metaprogramming is the ability to write programs that generate new programs or that change  the  behavior  of  existing  programs.

 

    In  an  OO  language  like  Ruby  or  Groovy, metaprogramming implies capabilities that extend existing object models, add hooks to alter the behaviors of existing methods (or even classes), and synthesize new methods,  properties,  or  modules  during  runtime  through  introspection.

    Languages  like Lisp  use  macros  as  the  metaprogramming  tool  that  let  you  syntactically  extend  the language during the compilation stage. Although the primary form of metaprogramming that's supported by Groovy or Ruby is runtime, Lisp metaprogramming is compile  time,  and  doesn't  incur  any  runtime  overhead.  (Both  Groovy  and  Ruby  have library support for compile-time metaprogramming through explicit manipulation of the  AST s. But it's nowhere near as elegant as Lisp.

    Java also offers  metaprogramming  capabilities  through  annotation  processing   and  aspect-oriented programming ( AOP); it also defines all its extensibility mechanisms through its  MOP.

    Statically  typed  languages  like  Haskell  and  OCaml  that  have  traditionally relied on pure embedded semantics for designing  DSL s now offer type-safe compile-time  metaprogramming  through  extensions  like  Template  Haskell  and  MetaOCaml respectively.  For  more  information,  see  http://www.haskell.org/th/  and http://www.metaocaml.org/

    Runtime metaprogramming in DSL implementation

    Why is metaprogramming support such an important feature for a language to host a DSL ? The answer is that because metaprogramming support makes a language exten-sible, the DSL  that you've implemented in an extensible language also becomes transi-tively extensible.

wps_clip_image-10150

你可能感兴趣的:(dynamic,protocol)