badarkta

More by badarkta
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 JATHA
[VIEWED 1790 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 200 days
Recommended Popular Threads Controvertial Threads
ChatSansar.com Naya Nepal Chat
TPS Re-registration case still pending ..
Toilet paper or water?
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
ढ्याउ गर्दा दसैँको खसी गनाउच
Sajha Poll: Who is your favorite Nepali actress?
Problems of Nepalese students in US
TPS EAD auto extended to June 2025 or just TPS?
whats wrong living with your parents ?
Mamta kafle bhatt is still missing
Ajay Kumar Dev sentenced to 378 yrs
Why Americans reverse park?
Now Trump is a convicted criminal .
TPS advance parole Travel document i-131, Class of Admission ?
Tourist Visa - Seeking Suggestions and Guidance
Nepali Passport Renewal
Biden said he will issue new Employment visa for someone with college degree and job offers
To Sajha admin
TPS Renewal Reregistration
मैले नबुझेका केहि गीत का lyrics हरु
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