Slashdot Mirror


Why Java Won't Have Macros

bugbear writes "Carlos Perez has just posted a page that quotes Sun Java 'theologist' Gilad Bracha about why there is no plan to add macros (in the Lisp sense) to Java."

14 of 140 comments (clear)

  1. in the Lisp sense? by Tumbleweed · · Score: 4, Funny

    No macroth? Thath too bad. ;)

  2. Interesting point. by alyosha1 · · Score: 5, Interesting
    The single best measure of whether a programming language is worth using is: how well does it support communities.
    This point doesn't seem to get raised very often when comparing the relative merits of language A against language B. It's easy to forget that while a language is a tool for communicating with a computer, it is also (and arguably more importantly) a tool for communicating with other programmers.

    Which is why Python is rapidly becoming my favourite language. I find other people's python code far easier to understand than stuff written in other languages. This despite the fact that I've only been hacking python for a few months, and I've been using C++ and other languages daily for several years.

    I feel that there's scope for yet another 'methodology', alongside OO and XP and all that - it might be called 'code as conversation', whereby the quality of a piece of code is judged by how readily it communicates intent to other programmers.
  3. Re:useless by Tumbleweed · · Score: 4, Funny

    > if u want the extra performance boost

    Maybe you wouldn't find macros so useless if you created some keyboard macros to replace ' u ' with ' you '.

    Just a thought.

  4. Re:useless by reynaert · · Score: 4, Informative

    No, in Lisp macro's aren't used for inline functions , but for syntax extensions. For a demonstration of what real macros can do, look at The Swine Before Perl. That presentation shows how easy it is to implement special syntax for automatons in Scheme, and how natural & simple the result looks.

  5. Sigh.. by DrunkBastard · · Score: 5, Funny

    One less language to be used in obfuscated code contests. Where's the fun without macros and overloading? sigh....

  6. Re:OOP by pauljlucas · · Score: 4, Insightful
    The problem with macros, is they sorta defeat Java's OOP
    Macros have nothing to do with programming methodology (OOP, procedural, functional, etc).
    Defining a symbol, just to be replaced in thousands of other places where it's written, tied only to the global space.
    Yes? So? What things are named or what text is substituted before the compiler looks at it has nothing to do with OOP.
    Sounds awfully like a procedure/function to me.
    Macros are text substitution or syntax tree manipulation alone. Macros are not called, so why you think they have anything to with procedures or functions?
    --
    If you reply, do so only to what I explicitly wrote. If I didn't write it, don't assume or infer it.
  7. Re:OOP by Dr.+Photo · · Score: 4, Informative

    The macros the article is talking about are Lisp-style macros.

    These are not your "shoot self in foot" C macros (i.e. replace text x with text y), but a very powerful and expressive way to have the entire language at your disposal at compile time.

    If you've seen the neat tricks you can do with C++ templates (template metaprogramming, etc), you might have an idea of what real macros would be like, when severely watered-down.

    Lisp macros make things like Generic programming and OOP very simple to add to a language, as well as almost any other conceivable programming construct.

    For instance, with proper macros in Java, you wouldn't ask, "When will templates be added to Java?"; you could add them yourself.

  8. Strange, I've been arguing about this all day ... by torpor · · Score: 4, Interesting

    ... and losing badly.

    Okay, C programmers, tell me why these 4 macros are bad:

    #define is ==
    #define and &&
    #define or ||
    #define until(a) while(!(a))

    Tell why they're good if you think they're good, but I'm only interested in the bad argument (having lost it all day). :)

    --
    ; -- the corruption of government starts with its secrets. a truly free people keep no secrets. --
  9. Re:Strange, I've been arguing about this all day . by reynaert · · Score: 4, Interesting

    They're bad because they don't extend the C syntax, they just change it. Good macros extend the syntax, but keep the new syntax in same style as the original language. If you want to know to what your four macro's lead, look at the famous Bourne shell source code. A few simple definitions like yours in mac.h result in the horror of xec.c and cmd.c.

  10. If this is what Jabba does, then Jabba will lose. by ArmorFiend · · Score: 4, Informative
    The crux of his argument is that user defined macros make code unreadable. If users are able to create their own macro constructs, they'll be "making their programs unreadable for everyone else". This is pure hogwash from someone that's probably never used hairy-chested lisp macros seriously. I'll demonstrate to you the hogwashishness in two phases.

    Flash-back to 1969 when the same arguments were put forward by assembly programmers against named functions:

    User defined functions make code unreadable. If users are able to create their own functions, they'll be "making their programs unreadable for everyone else".
    History has show that user defined functions are good. People are finally realizing that lisp macros are perhaps an equally important good. By taking this position, Jabba is positioning itself on the losing side of history.

    Seriously, what's easier to read:

    // macro-less call
    {
    window.context().glxMakeContextCurrent();
    float[4] color = window.fgColor();
    glColor(color);
    glPushMatrix() ;
    glIdent();
    glAlphaBlend( GL_FALSE );
    ...non-boiler-plate-code...
    glAlphaBlend( GL_TRUE );
    glPopMatrix();
    }
    OR
    // macro call
    withGLDrawing (color) {
    ...non-boiler-plate-code...
    }
    If you look at it in terms of lines of code, in terms of error-prone-ness, in terms of high-level versus low-level, in terms of maintainability, ONLY AN IDIOT WOULD CHOOSE AGAINST MACROS.

    Okay, sorry for being ornery, but mod this scruffy post up!

  11. Perl by (trb001) · · Score: 4, Insightful

    given this statement

    The real point is that LFSPs (Language for Smart People) have a much greater support for abstraction, and in particular for defining your own abstractions, than LFMs (Language for the Masses). ...has anyone declared Perl *the* LFSP? I can't think of a more abstract and unreadable language off the top of my head (Cobol doesn't count just because the younger generation has never used it).

    --trb

  12. Re:Strange, I've been arguing about this all day . by FroMan · · Score: 4, Interesting

    Programming languages are similar to spoken/written language, they are meant to convey an idea. In programming the ideas are instructions to the computer and to someone maintaining the code. For each language there are pedantic ways of doing things, usually from tradition or common ideas.

    In C when you are writing and infinite loop for a server, the standard way to do it is

    for (;;) {
    if ((acceptfd = accept(...)) < 0) { /* handle errors here */
    }

    if ((pid = fork()) < 0) { /* handle errors here */
    }

    if (pid == 0) { /* parent */
    } else { /* child */
    }
    }

    There are two very common C practices used in this. The first is the for (;;) {}. This is the standard way to do it because there is no comparison ever used within the loop, where a while (1) {} would seem to do the same thing it does have a comparison. Well, now it wouldn't matter since the compiler would optimize the loop anyways since the '1' is a constant, older compilers or non-optimizing compilers would not catch that though.

    The second practice is the if ((x = f()) < 0) {}. This arises more out of tradition. It gets the return value of a system call and allows the error handling to be done immediately. It also helps keep the indenting level from running away.

    Ammusingly enough in Java with JCSC I find that it gives warnings saying it is bad style. I would guess that it would prefer

    try {
    int acceptfd = accept(); // non error condition here
    } catch (Exception e) { // handle accept error here
    } finally { // no matter error or not here
    }

    From a C background I find this abhorrent. Look at the level of indentation required! Evil!

    Well, what does that have to do with #define or ||? Tradition. C programmers are used to seeing "||" as an "or". There is no keyword "until". If a C developer is hired new into the place he will have to learn the idiosyncracies that this place uses.

    Specific arguments against using macros to extend the language are that you are in effect adding keywords to the language. In effect "or", "and", "is" and "until" are now keywords in C. Since these are not part of the language normally, any code that uses any of those as a variable will now have issues.

    Well, who would use "or", "and", "is" as a variable name? Well, look at the linux kernel code and you will find that "new" is used occasionally, which causes the kernel to not be able to be compiled with a C++ compiler. I would also argue that "or" could easily mean outputReport and "is" quite often means inputStream to me.

    By using this "or", "and", "is" nonsense you are also leaving out the possiblity of using similar ideas for bit fields. You could use "xor", but you would need a "bor" for bitwise or'ing of a field.

    Basically what I see by using the macros here is that you are limiting yourself to a number of poor programming practices. When you use these you will be confusing real programmers and potentially inserting errors into the code. In C "||" is well defined and any C programmer is able to understand it. "or" on the other hand is ambigious to C programmers since it is not part of the programming language.

    --
    Norris/Palin 2012
    Fact: We deserve leaders who can kick your ass and field dress your carcass.
  13. Re:useless by Elwood+P+Dowd · · Score: 5, Funny

    >> if u want the extra performance boost

    >Maybe you wouldn't find macros so useless if you created some keyboard macros to replace ' u ' with ' you '.
    >
    >Just a thought.

    Maybe yoyou woyouldn't find macros so youseless if yoyou created some keyboard macros to replace 'you' with 'yoyou'.

    Jyoust a thoyought.

    --

    There are no trails. There are no trees out here.
  14. Java needs Macros, Badly by __past__ · · Score: 4, Insightful
    Lisp-style macros are, in a sense, all about syntactic sugar. They let you write stuff easily and predicably that would be possible, but inconvenient by hand, to the extent that it becomes natural to implement domain-specific languages with them.

    Java, and about every non-Lisp language, lacks syntactic extensibility. Yet, Java programmers obviously consider Java syntax to be not usable for a lot of everyday tasks - hence the ever-growing number of special mini-languages around Java, like Ant-XML, XDoclets, JXPath, to a degree even XSLT.

    Of course it is possible to implement such DSLs in Java, but you typically end up writing an interpreter, and that usually means that, if you want to use the DSL in your program (say, evaluating expressions built at runtime), you end up manipulating strings and feeding them to a huge, opaque interpreter machinery - which is error prone and the hell to debug. And, by the way, one of the things people often call one of the biggest problems of macros, when they only know C-style ones.

    Basically, much of what macros allow you to do is already done in the Java world, just without the language supporting it, and hence in slow, buggy and hard-to-debug, often ill-specified ways.