>for(int i : 1..5)
>instead of
>for(int i=1; i++; i<=5)
The Java way to do this might be
public class Main
{ public static void main( final java.lang.String[] args )
{ for( final int i : new de.dclj.ram.system.iteration.IntegralRange( 10, 12 ))
java.lang.System.out.println( i ); }}
10
11
12
This uses my library ram.jar
http://www.purl.org/stefan_ram/pub/ram-jar
The class is being described incompletly here (source code
of the class can be reached from there):
http://www.purl.org/stefan_ram/html/ram.jar/de/dclj/ram/system/iteration/Integra
lRange.html
The object oriented approach might be:
public class Main
{
public static void loop( final int from, final int to,
final de.dclj.ram.event.Accept<java.lang.Integer> acceptor )
{ for( int i = from; i < to && acceptor.accept( i ); ++i ); }
public static void main( final java.lang.String[] args )
{ loop( 12, 14 + 1, new de.dclj.ram.event.Accept<java.lang.Integer>()
{ public boolean accept( final java.lang.Integer i )
{ java.lang.System.out.println( i ); return true; }}); }}
using
http://www.purl.org/stefan_ram/html/ram.jar/de/dclj/ram/event/Accept.html
pintman - 04 Aug 2006 20:34 GMT
> >for(int i : 1..5)
> >instead of
[quoted text clipped - 6 lines]
> { for( final int i : new de.dclj.ram.system.iteration.IntegralRange( 10, 12 ))
> java.lang.System.out.println( i ); }}
As you always see in Java there is a great verbose blowup that I wanted
to get rid off. Of course there are ways to DO the same but macros are
a way to WRITE it down in a way that does the same.
But thanks for your supporting libraries.
Greetings,
Marco.
>> Hm, what do you need that for? I've never felt the need for that in
>> several years of doing Java. IMHO there are superior mechanisms that
>> also have the added advantage of not breaking tool support.
>
> With macros its possible to create new abstraction layers into the java
> programming language without the need of parsers.
True, although I have to say I always feel wary about preprocessing. I
think Stroustrup regretted not having got rid of CPP in C++ for a reason...
Do you have concrete examples where the benefits of macro processing far
outweigh the disadvantages?
> For instance it would
> be possible to create Singletons (or other paradigms) without the need
> of knowing implementation details.
Hm, I've never felt it too much typing to do that pattern myself.
> Another example is the usage of
> shortcuts for syntatic constructs. If I need to handle a lot of
[quoted text clipped - 5 lines]
>
> for(int i=1; i++; i<=5)
In this particular example I don't see improvements in readability and
the breaking of tool support would definitively make me not use a macro
for this.
> As one could see in LISP there are a lot of applications for this.
I guess Lisp is a bit different because the macro facility is so tightly
integrated with the language (and very powerful indeed). But YMMV.
Thanks for giving a bit more background on your motivation!
Kind regards
robert
Stefan Ram - 04 Aug 2006 09:26 GMT
>Do you have concrete examples where the benefits of macro processing far
>outweigh the disadvantages?
I have a license (the GPL) at the top of each of hundreds of
Java source files. This is inserted during the make process
with a preprocessor. I'd just need to change the file with
preprocessor definitions at a single place to change all
source files to incorporate another license.
An example of a generated Java source file is:
http://www.purl.org/stefan_ram/java/de/dclj/ram/system/iteration/IntegralRange.java
It also contains
/** (...)
@version slr@2006-06-05T07:51:48+02:00
@see <a href="http://(...)IntegralRange.java">source code</a>
@see <a href="http://(...)IntegralRange.html">documentation</a>
@see <a href="http://(...)ram-jar">Library homepage</a> */ (...)
@de.dclj.ram.meta.description.Cleared("slr@2006-06-05T07:51:48+02:00") (...)
public class IntegralRange
So, links to the source code, the documentation and the
library home are inserted into every source file and
documentation file. This is also done by the preprocessor,
which inserts the text "IntegralRange" at all the three places
where it is used above. Also, the same version date is
inserted in both the JavaDoc comment and an annotation.
To do this manually, would be less consistent across source
files, more work, and more error prone.
Robert Klemme - 04 Aug 2006 17:37 GMT
>> Do you have concrete examples where the benefits of macro processing far
>> outweigh the disadvantages?
[quoted text clipped - 28 lines]
> To do this manually, would be less consistent across source
> files, more work, and more error prone.
True. Personally I stick with the class doc templates eclipse provides.
But this doesn't allow for easy changes of course.
Kind regards
robert