search

Home  >  Q&A  >  body text

Ruby的元编程(Metaprogramming)在其他语言中是否有相似的技术?

元编程:编写在运行时操纵语言构件的代码。
--《Ruby元编程》

应该是借鉴了lisp里的宏(macro)。其他语言里还有类似的用法吗?

黄舟黄舟2774 days ago872

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-22 09:01:06

    Let’s put it this way:
    In the early days of the industrial age, this was the situation:
    People produce machines, and machines produce products.

    At the later stage:
    People produce machines, machines produce machines, and machines produce products.

    Obviously, machine production machines are a great progress, which greatly liberates the labor force.

    Metaprogramming is similar to machines producing machines. Simply put, programs can change themselves or generate new programs. This of course greatly liberates programmers.

    Of course, in fact, before metaprogramming, programmers invented a large number of auxiliary tools to help them program, such as IDEs and the like. But later, programmers felt that the IDE was still too weak, so they could search and replace, jump to reference and apply templates, etc. It would be great if the program could produce itself.

    ====================The science popularization is completed, here are the irresponsible thoughts==================== ==

    In fact, meta-programming has never been a well-defined concept, mainly because the meaning of the word meta itself is very vague.

    Yuan is generally understood as more basic and basic. Metaprogramming is programming that is more basic than programs, that is, programs generate programs. From this perspective, whether it is C++ templates, C macros, .NET's Emit and Compiler Provider, JavaScript's string splicing and eval, or even the Dongwang code generator, they are all program generation programs.

    But generally speaking, to be called metaprogramming, certain conditions must be met. Of course, since the concept of metaprogramming is not well defined, these conditions can only be regarded as consensus. It is normal for the features of this language to be regarded as metaprogramming by person A, but not by person B. The conditions listed below are just from my perspective:

    1. A language with metaprogramming capabilities must be a language that can generate codes in the same programming language, rather than using one language to generate programs in another language. This takes code generators out of the equation.
    2. For languages ​​with metaprogramming capabilities, the program code generated by metaprogramming must be able to pass lexical analysis. This article excludes C macros and JavaScript eval.

    It can be seen that it is considered metaprogramming after all, and different results can be obtained by using different constraints. When we discuss whether a programming language can be metaprogrammed, we can basically reach a consensus on condition 1, but everyone has their own opinions on condition 2.

    It is said that when metaprogramming was first created, it was simply to reduce the burden on programmers. C++ templates were initially just an extension of overloading. Due to strong typing, C language must write a lot of repeated code for different types. Overloading solves the name pollution of these repeated codes, and generics begin to solve the duplication of these codes. But the guys in the C++ Standardization Committee obviously will not stop at generics like Anders. High-end atmosphere and class have always been the unremitting pursuit of C++, so generics have been turned into templates. Furthermore, programmers have also come together. Happy, I came up with C++ template metaprogramming.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-22 09:01:06

    Javascript is there!

    http://www.slideshare.net/danwrong/metaprogramming-javascript

    Pay attention to the publication date of this presentation. This is nothing new. With the continuous improvement and expansion of languages ​​over the years, more metaprogramming techniques have been widely used. You can google it.

    In addition, languages ​​that support metaprogramming are very common, almost all functional programming languages ​​are available, Java has Annotation, and Clojure goes without saying. Lisp is based on macros, and Ruby and Smalltalk both mainly use reflection mechanisms. There are also things like Groovy that are based on byte encoding and AST conversion, etc.

    Metaprogramming is an advanced skill, but it is not magic or a rare property.

    reply
    0
  • Cancelreply