Parameterizing on incomplete types

Jamison Hope jrh@theptrgroup.com
Thu Dec 1 00:24:00 GMT 2011


Hello,

In Java, I can write a class like this:

> public class Foo implements Comparable<Foo>
> {
>   public int x;
>
>   public int compareTo(Foo o)
>   {
>     return x - o.x;
>   }
> }


and it does the Right Thing, even though "Foo implements  
Comparable<Foo>"
seems to have a chicken & egg problem.


However, Kawa seems to be tripped up by such a thing:

> #|kawa:1|# (define-simple-class Foo (java.lang.Comparable[Foo])  
> (x ::int) ((compare-to (o ::Foo)) ::int (- x o:x)))
> /dev/stdin:1:47: unrecognized parameter type Foo

Is there a syntax that will work here? Scheme doesn't have a notion of
forward declarations of types, so I'm guessing the real solution is to
make BracketApply#rewrite() or object#rewriteClassDef() lazier?


Even worse, sometimes you want to be able to have multiple classes  
defined
as being parameterized on each other, or one class to be parameterized  
on
its own subclass (crazy but handy). For instance, JNAerator will  
translate
this C typedef:

> typedef struct { int x; int y; } Point;


into something like (with some parts omitted):

> public class Point extends Structure<Point, Point.ByValue,  
> Point.ByReference> {
>   public int x;
>   public int y;
>
>   public static class ByReference extends Point implements  
> Structure.ByReference {}
>   public static class ByValue extends Point implements  
> Structure.ByValue {}
> }

where the two auxiliary classes are used for different C calling  
conventions.

I don't think you can nest define-simple-class statements, can you? So  
the closest
thing would be something like

> (begin (define-simple-class Point (Structure[Point Point_ByValue  
> Point_ByReference]) ...)
>        (define-simple-class Point_ByValue (Point Structure$ByValue))
>        (define-simple-class Point_ByReference (Point Structure 
> $ByReference)))

but Structure[Point Point_ByValue Point_ByReference] is not well- 
defined until
all three classes have been defined. Can we make Kawa's compiler lazy  
enough
or multi-pass enough to make sense of that?






And yes, I can avoid the problem by using raw (unparameterized) types,  
but
that's cheating.

Thanks,
Jamie

--
Jamison Hope
The PTR Group
www.theptrgroup.com





More information about the Kawa mailing list