MACROS in Java

Programming, for all ages and all languages.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

MACROS in Java

Post by Neo »

Is there anyway of writing MACROS (or something similar) in Java?
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:MACROS in Java

Post by Neo »

Is there anything like this in Java at all?
Only Human
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:MACROS in Java

Post by Solar »

Not to my knowledge.
Every good solution is obvious once you've found it.
mystran

Re:MACROS in Java

Post by mystran »

No. No macros in Java.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:MACROS in Java

Post by distantvoices »

Shouldna be a problem thou'. Well ... No Macro, No Cry.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:MACROS in Java

Post by Candy »

the official reply is that you can do anything you'd want to do with a macro using inline functions.

the unofficial reply is that you cannot do anything with them, but that you'd have to hack your way around a few things

the hacker (original meaning, not "website defacer" meaning) reply is to plain use the gcc preparser if you want macro's. Try -E. It should be able to skip across most java stuff. Note, this also allows real includes.
Schol-R-LEA

Re:MACROS in Java

Post by Schol-R-LEA »

The only form of macro that is actually part of the Java language are the generics introduced in version 1.5, which are just similar enough to C++ templates to piss off a lot of C++ fans for the ways that they differ. Or so I understand; I've only taken a quick glance at them so far.

If you really insist on it, you can use standard C macros and simply run cpp over the source code before compiling it. Just don't be surprised when the other programmers lynch you for it.

There are other standalone macro processors like m4, most of which are light-years beyond cpp, but you'd still have the problem of them being non-standard. Even if it's only for code you never expect anyone else to use, it probably is a bad idea; at the very least, you'd want to make sure it was very clearly marked that you need to run the preprocessor over it before compiling. Also, sone Java-based languages such as AspectJ are basically macro processors, but then you're not really working in Java at all.
creichen

Re:MACROS in Java

Post by creichen »

Hi,

AspectJ etc. may be useful solutions, depending on what you're trying to do; perhaps it would be best if you were to explain to us what the problem is that you're trying to solve with macros: There may well be alternate solutions that do not require meta-programming (besides, Java has many trapdoors to allow meta-programming, though only dynamically).

Wrt Generics and C++ templates: I beg to differ. Those are vastly different things, they just happen to have similar syntax. C++ templates are glorified preprocessor macros, whereas C++' Generics are a methodology for introducing parametric types (-> parametric polymorphism), a concept which does not exist in C++.

As an example, if you want to use a "linked list" class, there are basically three approaches to keeping it in such a way that it works for arbitrary types (i.e., is polymorphic over a type parameter):

(1) The static C/C++ way: You can't do it, so you do a peprocessor hack (involved #defines in the case of C-- see FreeSCI's "hashmap" for an example-- and templates for C++) instead. The net result is that for every type you use, you have to re-compile (and re-typecheck) the container.

(2) The "old" Java way and the approach taken in dynamically typed languages like Smalltalk or Python: You cast everything to some base type ("java.lang.Object" in old Java) and cast it back when you retrieve it from the container. This also works in C and C++, typically by typecasting to a "void*". This approach only requires one compilation, but requires you to do dynamic type checking (or to run into funky run-time errors, in the case of C or unsafe C++ casts). Thus, it is slower than approach (1) and less safe.

(3) Parametric Polymorphism (e.g., Java Generics): You allow types to have "type parameters"; so there is List-of-int vs. List-of-String etc., just as in (1) (meaning that you get good static type-checking), but you only need to compile once. Performance varies; depending on the implementation, it tends to be somewhere between (1) and (2). Java's implementation is, unfortunately, a bit stupid (based on type erasure), so it's about as slow as approach (2).

Note that this isn't all there is to parametric polymorphism. There's also the notion of /bounded/ parametric polymorphism-- supported by Java-- which allows you to specify that you "take a type parameter if it is a subtype of T". For example, if you want to have a sorted list type, you can require your element types to be of a subtype of "java.util.Comparable" (or whatever it's called) and thus specify an order. Again, all of these things can be checked at compile-time, which reduces the time you need for testing your code.

Historically, parametric polymorphism comes from the world of functional programming (ML, then Miranda, then Caml, then SML, then Haskell, then Clean, if I'm not mistaken about the order in which those were first released); but I'm getting very off-topic now.

Sorry about this. But the "Generics are like templates" discussion is a frequent misunderstanding, and, with type systems being among my favourite topics, I couldn't leave this sitting around here as it was ;-)

-- Christoph
mystran

Re:MACROS in Java

Post by mystran »

Real hackers use M4 for all their macro needs, except when they are programming in Lisp, and have REAL (structural) macros at their disposal. Anything less than Lisp macros is **** anyway, so I don't see why people complain about Java not having (sucky, textural) macros.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:MACROS in Java

Post by Candy »

mystran wrote: Real hackers use M4 for all their macro needs, except when they are programming in Lisp, and have REAL (structural) macros at their disposal. Anything less than Lisp macros is **** anyway, so I don't see why people complain about Java not having (sucky, textural) macros.
Because some people don't need a chain saw to cut through a table leg.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:MACROS in Java

Post by distantvoices »

[ot]@candy:I'd use a jig saw for that kinda job. ];->. Honestly, a fox tail saw would suffice too, but we are lazy, aren't we?[/ot]

I've done quite some java programming. In all the Time I have not yet encountered the need for #define. java has that much nifty features that you needn't ressort to macros.

besides: isn't the 1.5 version said to be slimmer and faster?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:MACROS in Java

Post by Candy »

beyond infinity wrote: [ot]@candy:I'd use a jig saw for that kinda job. ];->. Honestly, a fox tail saw would suffice too, but we are lazy, aren't we?[/ot]

I've done quite some java programming. In all the Time I have not yet encountered the need for #define. java has that much nifty features that you needn't ressort to macros.
The point behind the chain saw was that you don't always need the biggest & baddest tool to do something. To get my kids to school I don't require a ferrari spider 360. It could be used, but it would be just as OK with a smart (to name another extreme). Yet, the second is less heavy and more commonly used for the purpose. Plus, if it breaks down, more people know how to fix it again since it's common. Don't use M4 for the same stuff plain C macros can do, if you really don't need all the other features.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:MACROS in Java

Post by distantvoices »

I've got the point immediately, candy. *gg* I'm not that bad in english. I've just jested around a bit. ;-) My mood is in hilarious mode and gambols around like a happy hare.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:MACROS in Java

Post by Neo »

I downloaded M4 for windows and have tried using it.
!'ve defined a macro which is something like this.

Code: Select all

define(`test',`$1, $1.drawOutput();'
When I use it I call it like this

Code: Select all

test(arg1)
but i get an output of
arg1, arg1.drawOutput();
How do i remove the first 'arg1' from that line? I need only arg1.drawOutput();
Only Human
AR

Re:MACROS in Java

Post by AR »

This is just a guess, but assuming

Code: Select all

define(`test',`$1, $1.drawOutput();'
$1 refers to the first parameter, wouldn't it be:

Code: Select all

define(`test',`$1.drawOutput();'
?
Post Reply