[Show all top banners]

badarkta

More by badarkta
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 JATHA
[VIEWED 1691 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
TPS Re-registration case still pending ..
TPS Re-registration
What are your first memories of when Nepal Television Began?
निगुरो थाहा छ ??
ChatSansar.com Naya Nepal Chat
Basnet or Basnyat ??
Sajha has turned into MAGATs nest
NRN card pros and cons?
Toilet paper or water?
TPS EAD auto extended to June 2025 or just TPS?
Do nepalese really need TPS?
Biden out, Trump next president, so what’s gonna happen to TPS, termination?
कता जादै छ नेपाली समाज ??
मन भित्र को पत्रै पत्र!
Will MAGA really start shooting people?
Democrats are so sure Trump will win
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
Tourist Visa - Seeking Suggestions and Guidance
Top 10 Anti-vaxxers Who Got Owned by COVID
I regret not marrying a girl at least for green card. do you think TPS will remain for a long time?
Nas and The Bokas: Coming to a Night Club near you
Mr. Dipak Gyawali-ji Talk is Cheap. US sends $ 200 million to Nepal every year.
Harvard Nepali Students Association Blame Israel for hamas terrorist attacks
TPS Update : Jajarkot earthquake
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