Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why can't macro systems be part of an IDE? Is there anything macros do that can't be done at compile-time?

Startup idea: web-based language-specific IDE, with a simple interface for sharing/voting on macros between users.



The lisp macro system can more or less be done at compile time, yes, but that's not the problem with other languages. The format of lisp makes it especially easy to do macros - code is data!

Here's an example. Take, for example, this class declaration:

  (defclass dog (animal)
    ((size :accessor dog-size)
     (name :accessor dog-name :init-form "Fido")))
How do you make that into data? Easy. Just quote it:

  '(defclass dog (animal) ...)
Now take the equivalent in Java:

  public class Dog {
    int size;
    String name = "Fido";
  }
How do you get that into data? Well, I suppose the best you could do without modifications to the core language is just put it in a string. But then to do something useful you'd have to do so much tedious string parsing it wouldn't be worth it. I suppose you could invent a special ParseTree class or something, and a quote operator, and etc etc, but that wouldn't be nearly as easy as Lisp makes it.

What the previous poster was saying is that once other languages have a code representation that is easy to work with as data, and can manipulate that code as data at compile-time to produce complex macros, then Lisp might fade. (At least that's how I understood it, correct me if I'm wrong.)


Or:

1. Allow a factory for Class.

2. Make Method contain the full body contents as a list of statements, and give them a factory as well.

Then it's a matter of reasonable constructor syntax.


And a whole lot of "wait, what tree does this code map to again?"

With lisp, there's no difficulty mapping:

   int main(int x) {foo()}
to

  Method m = new Method(return:=new Type(int),
                        args  := new ArgList(
                                  new Arg(new Type(int),
                                          new Name("x"))),
                        body  := new Block(
.... I'm sure you get the point....

The syntax tree is apparent from the syntax.

  (+ 1 2 (* 3 4))
Maps to:

  '(+ 1 2 (* 3 4))


While people generally talk about using macros for code generation, macros are essentially for doing things at compile time. (Or read time, in the case of read macros, e.g. 'x -> (quote x).) Due to simplicity of Lisp syntax, the easiest way to extend the language is usually by generating code, though, so that's what they're associated with.

You can do things with the C preprocessor at compile time, it's just completely unaware of C, which really cripples it. The Lisp preprocessor (its macro system) is also running Lisp, and can easily process itself.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: