badarkta

More by badarkta
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 JATHA
[VIEWED 2235 TIMES]
SAVE! for ease of future access.
Posted on 03-01-08 8:16 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Jatha

Bringing to Java some of the power of Lisp

Introduction

Lisp programmers have a very powerful macro system. When a Lisp programmer writes a macro it's not like a C programmer's macro; it's not just a simple textual substitution. No, when a Lisp programmer writes a macro, she has the full power of Lisp at her disposal.

Lisp macros are Lisp programs that write Lisp programs.

Jatha macros are Java programs that write Java programs.

Jatha is a preprocessor that runs arbitrary java code in order to generate the source code that the compiler sees. Using Jatha, I wrote a simple @PROP macro, that turns this:


class Foo { @PROP(String, name); }

Into this:


class Foo { private String m_name; public String getName() { return m_name; } public void setName(String name) { m_name = name; } }

Jatha "macros" are actually just regular java classes. The @PROP macro is merely a class named "PROP" that lives in the "macros" package. It has a method named expand() that can turn an argument array like {"String", "name"} into the source code for a property with get and set methods.

I have written other simple macros. The @ENUM_OBJ macro turns this one-liner:


@ENUM_OBJ(MyEnumeration, INT, "int", DOUBLE, "double")

Into this sophisticated object-based enumeration:


class MyEnumeration { public static final MyEnumeration INT = new MyEnumeration("int"); public static final MyEnumeration DOUBLE = new MyEnumeration("double"); public static int getNumElements() { return 2; } public static MyEnumeration getElement(int __index) { switch (__index) { case 0: return INT; case 1: return DOUBLE; default: return null; } } public static MyEnumeration getElement(String __name) { if (__name.equals("int")) return INT; if (__name.equals("double")) return DOUBLE; return null; } private MyEnumeration(String __description) { this.__description = __description; } private String __description; public String toString() { return __description; } }

You can write macros like these too. Try it. You'll like it.


 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 365 days
Recommended Popular Threads Controvertial Threads
Conservative discussions
TPS Re-registration case still pending ..
TPS for Nepal likely to extend next week
सालीको चाक
ChatSansar.com Naya Nepal Chat
TPS for Venezuela is terminated, only 60 days extension for transition period
TPS to F1 Status.
TPS To F-1 COS
Venezuela TPS lawuit
Toilet paper or water?
tesla stock OMG !!
Those who are in TPS, what’s your backup plan?
Biden out, Trump next president, so what’s gonna happen to TPS, termination?
TPS Sakiyo Tara Case is in Court.
Got my F1 reinstatement approved within 3 months(was out of F1 for almost 2 years)
Homeland Security revokes temporary status for 532,000 Cubans, Haitians, Nicaraguans and Venezuelans
Has anyone here successfully reinstated to F-1 status after a year-long gap following a drop from F-1?
I hope all the fake Nepali refugee get deported
Court Hearing Approval.
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
Looking for girl
Who is hottest nepali female?
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters