From: Ken A. <kan...@bb...> - 2004-01-26 12:40:05
|
Jython code can be more compact than Java's for several reasons. One is you don't need type declarations. But this doesn't help much if you put one assignment on a line, though it does help with really long ones. The other thing that helps is that you can do constructors with keyword arguments. Tim's JLib has a nice variant on this where the construct guesses which setter should get which argument based on its type. Something like this: (button "Quit" Color.red$) - to make a red JButton labled "Quit". For this to work, you need to define button and other procedures that parallel what Java offers. Here's an alternative (with) that uses methods as keywords: (with (FTPClient. remoteMachine) .debugResponses #t .login user password) This is equlivalent to 4 lines: (let ((it (FTPClient. remoteMachine))) (.debugResponses it #t) (.login it user password) it) Here's a procedural version, though we could do a macro version that would do most of the work at read time. (define (with what . kvs) (define (method? x) (instanceof x JavaMethod.class)) (define (op kvs) (if (null? kvs) what (if (method? (car kvs)) (args (car kvs) '() (cdr kvs)) (error {expected operator, but got [(car kvs)]})))) (define (args method sofar kvs) (if (null? kvs) (begin (apply method what (reverse sofar)) what) (if (method? (car kvs)) (begin (apply method what (reverse sofar)) (op kvs)) (args method (cons (car kvs) sofar) (cdr kvs))))) (op kvs)) |