(define-class)
The macro (define-class) lets you define Java class in
Scheme. To Java, such a class looks just like and other Java class.
The only difference is that its behavior is implemented in Scheme.
The classes in this directory are simple metaclasses used by
(define-class). These classes are organized more
consistantly than the java.lang.reflect classes are. Their behavior
is written in JScheme.
Example
For example, to sort a vector of integers, say, #(5 2 1 3 9 6 2 7
1)
using Arrays.sort, you need an implementation of
java.util.Comparator. This can defined like:
(define-class
(package frog)
(import java.util.Comparator)
(public class Compare implements Comparator)
;; Design issue, fields must be public for Jscheme code to access.
(public Procedure predicate)
(public Compare (Procedure predicate)
(.predicate$ this predicate))
(public boolean predicate (Object a Object b)
((.predicate$ this) a b))
(public int compare (Object a Object b)
(cond ((.predicate this a b) -1)
((.predicate this b a) 1)
(else 0)))
(public boolean equals (Object that)
(and (eq? (.getClass this) (.getClass that))
(eq? (.predicate$ this) (.predicate$ that))))
(public int hashCode () 0))
You can then sort the vector with:
(let ((a #(5 2 1 3 9 6 2 7 1)))
(Arrays.sort a (frog.Compare. <))
a)
which produces:
#(1 1 2 2 3 5 6 7 9)
The (define-class) macro looks similar to the equivalent
Java class. It consists of a sequence of clauses.
The clauses can occur in any order, though it is convention to follow
an order close to Java's.
define-class syntax
define-class = "(" "define-class" head member* ")"
head = package import* class|interface
package = "(" "package" PACKAGENAME ")"
import = "(" "import" FULLCLASSNAME|PACKAGENAME ".*" ")"
class = "(" modifier* "class" extends? implements? ")"
interface = "(" modifier* "interface" extends? ")"
extends = "(" "extends" CLASSNAME+ ")"
implements = "(" "implements" CLASSNAME+ ")"
member = constructor|field|method|static
constructor = "(" modifiers* name "(" arg* ")"
exp* ")"
field = "(" modifiers* type name (= value)? ")"
method = "(" modifers* type "(" arg* ")" exp*")"
static = "(" static exp* ")"
type = Java type.
value = exp
arg = type name
exp = {Scheme expression}
name = {Java identifier}
Issues
- (this) and (super) in constructor.
- (.foo super ...) in methods.
- Unnecessary recompilation.
- Java class redefinition.
- Debugging errors. Adding comments to java file?
Scheme files
Scheme files in this directory include:
|