I was trying to use cl-prevalence today. As a practice, I tried to define a deftransaction macro, just the one like in the bknr-datastore package. I didn't choose the latter, because in Slime, the transactions always get stuck.
Suppose you're using prevayler in Java. It has a transaction interface. Everytime you want a new transaction, you have to define a class, write the same string "implements Transaction", along with the prototype of the transaction execution method. What if you can make transaction something more like class? Just like "enum", raised to the level of a keyword? Or, rather a function declarator like synchronized, denoting the function to be expanded in to a Transaction subclass.
If you can write:
public transaction int myTransaction (int arg1, int arg2) {
return arg1 + arg2;
}
Life would be so much easier. The compiler then has to compile this piece of code into a Transaction subclass. I don't know if the Java 5 annotation can do this. I suspect that the decorators in the latest Python versions can do this. I doubt any Java-nik can do this trick properly.
Well, I'm sure that Lisp can do this. I wrote a 'deftxn' macro, and now I can define transactions just like I define functions (with defun substituted by deftxn of course). I can call the transaction just like I call ordinary functions: actually, they are ordinary functions. The deftxn macro actually defines such a function for me.
This practice brings me one step closer to the so-called "Metaprogramming". And Lisp is the king language in this field. Wish me luck : )
P.S.: the deftxn code:
(in-package :clp-ext)
(defmacro deftxn (name (system &rest args) &body body)
(dolist (arg args)
(when (listp arg)
(error "can't have argument defaults in transaction declaration for transaction ~A, \
please implement a wrapper" name)))
(let ((args-name (gensym))
(tx-name (intern (string-upcase (concatenate 'string "tx-" (symbol-name name)))
(symbol-package name))))
`(progn
(defun ,tx-name (,system ,@args)
,@body)
(defun ,name (,system &rest ,args-name)
(execute ,system
(apply #'make-transaction ',tx-name ,args-name))))))
No comments:
Post a Comment