views:

232

answers:

7

What's the best language (in terms of simplicity, readability and code elegancy) in your opinion, to learn and work with metaprogramming?

I think metaprogramming is the "future of coding". Not saying that code will extinct, but we can see this scenario coming on new technologies.

+2  A: 

Ruby has very powerful and flexible metaprogramming capabilities.

Adam Crossland
Ruby keep being hailed as a meta-language, but it does not have a well-structured meta-programming framework - It just uses its dynamic nature, like Python. IMHO learning meta-programming deserves a review of meta-classes and self-reflective languages, even if the common practice is to use a hash-based dynamic language.
Little Bobby Tables
A: 

Don't know if we have the same definition of "meta programming" but there is certainly not ONE best language to learn. I would propose that you have a deeper look at functional programming. Which language to choose for that depends on your background and working environment. I would choose F# at the moment, but Haskel should also be a good choice.

cheers, Achim

Achim
+1  A: 

What do you mean by metaprogramming? Metaprogramming is a set of concepts, rather than one specific technique.

See this answer where I've listed various concepts and related languages. Here is a summary:

  • Metaprogramming with macro --> Lisp
  • Metaprogramming with DSL --> Many languages for internal DSL, external DSL is more tricky
  • Reflection --> Smalltalk, Ruby
  • Annotations --> Java
  • Byte-code or AST transformation --> Groovy

See the complete answer for more details. Generally speaking, I think that a good OO all-rounder is Ruby. Otherwise any Lisp-like is will do the job: it's like putty in your hands. But that will depend on what you want to do...

ewernli
Thinking about enterprise apps and the set of concepts, not just a technique, but all that can be done with metaprogramming. I mean, using an easy to work with and learn technology. Ruby seens to be a good choice.
Erup
As @Jay listed, the syntax metaprogramming technique seens to be better in an enterprise enviroment, in my opinion. Manipulating at runtime isn't suitable, sometimes (like Ruby?).
Erup
+1  A: 

The Lisps are pretty much the language of choice for a wide variety of metaprogramming techniques. Of the modern Lisps available, I would recommend Clojure as a more accessible Lisp that has access to a positively HUGE library (anything in Java land) if you want something that is both powerful and immediately useful.

For other approaches to metaprogramming almost any functional language will do the trick. Haskell is a good choice for learning techniques and functional programming but isn't what I'd call the most practical language to do real work in at this time. Erlang is more practical, but not quite as amenable to metaprogramming. OCaml is another possible choice but suffers a bit on the practicality front as well. It is more accessible than Haskell in many regards, however.

In the scripting language world Ruby is a language in which metaprogramming is a popular technique. Its approach is vaguely Lisp-like, but with a far more conventional syntax. It lacks the full power and flexibility of the Lisps, however, but on the other hand, with the exception of Clojure above, it has a lot more immediate practical utility.

JUST MY correct OPINION
A: 

There are several languages that I would recommend for studying meta-programing.

The first is Prolog. A Prolog program is a database. Prolog "code", the clauses, are part of the data. The program can read them, including their content. It can also generate new code as a data structure and assert it, thus changing itself on run-time. All of this without using term expansion, which is Prolog's smart macros system. Some Prolog AI books start with implementing a meta-interpreter in Prolog, and then changing it by need.

The second is, as mentioned, Lisp, and particularly CLOS (Common List Object System), which includes commands for meta-OOP.

Finally, Python support a nice and not too obscure mechanism for run-time meta-programming, which is it's meta-classes (classes that create classes).

Little Bobby Tables
+1  A: 

First -- I don't think I agree with your claim that "metaprogramming is the 'future of coding'". It's a great tool, but not everybody likes it (for example, the Java designers left macros out of the language intentionally -- not that I like Java, but people do have reasons to object to metaprogramming).

Anyway... I can think of two different ways of doing metaprogramming: on the syntatic level and at runtime.

For syntax metaprogramming, I think Scheme is a good option (if you hadn't mentioned simplicity etc I'd suggest Common Lisp). For runtime metaprogramming I guess both Prolog and Smalltalk are very interesting. (You can add, change and remove facts to a Prolog database on the fly; and you can change Smalltalk objects on the fly to). You can probably do runtime metaprogramming in Ruby too, but I don't know Ruby.

So --there are several different metaprogramming methods in Scheme (different macro systems). I suggest you take a look at some basic Scheme book and later read about two different macro systems.

Some good Scheme books:

Scheme implementations are very different from each other, so you'll also use your Scheme implementation manual a lot too.

Some places to learn about Scheme macros:

If you decide to use a language that's larger and messier than Scheme, try Common Lisp. There are three books that I'd suggest:

  • First, "Practical Common Lisp" by Peter seibel. That will get you started on Common Lisp and macros;
  • Second, "On Lisp" by Paul Graham. You'll then learn that macros are more powerful than what you had thought before, and will learn really nice techniques;
  • Third, "Let Over Lambda" by Doug Hoyte. An advanced book, best read after Graham's On Lisp.

For Prolog, you can read "Programming in Prolog" by Clocksin and Mellish (get the latest edition!) and later move on to "Prolog Programing in Depth" by Covington, Vellino and Nute. See chapter 6.

There are lots of good Smalltalk books. I like "The Art and Science of Smalltalk" by Simon Lewis. There's a very nice free tutorial/primer by Canol Gokel about Smalltalk too (but it doesn't go as far as teaching metaprogramming).

Jay
I liked the way you covered the topic. Also, thinking about enterpise apps and the set of concepts, the implementation and maintainability (working on a team), I understand that syntax metaprogramming tech (as you described) is a better choice over runtime.
Erup
@Erup: Don't study concepts using a language that your team will use now. Study concepts to learn the concepts, using whatever tool helps best, then pick your language once you understand the concept. You can do metaprogramming in C++, for example, if you're very good with templates, but LEARNING metaprogramming through C++ would be ... non-trivial.
JUST MY correct OPINION
Thanks @JUST MY correct OPINION, learning the concepts is what I want. When I said "working on a team", I mean the ideia of using a tech that will be easy to work with. All the team will have to learn!
Erup
+2  A: 

I'm surprised no one has mentioned ML. ML stands for Meta Language. so... yeah... CaML is a standard implementation. (OCaML, which JUST MY correct OPINIO mentioned is the OO version of CaML, which probably adds features that make the meta-programming less obvious...)

Other than that, I am a big fan of Scheme, but pretty much any Functional programming language is good for this... There's always the Little Lisper, er, sorry, the Little Schemer...

Brian Postow
People who program in OCaml tend to drop the O part really, really quickly, leaving behind only the Caml. I don't think any serious OCaml programmer uses the object-oriented portions of it with any frequency.
JUST MY correct OPINION