This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Parameterizing on incomplete types


On 11/30/2011 04:23 PM, Jamison Hope wrote:
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?

The attached patch solved the name-lookup part of the problem.


I now get:

string>:1:27: type parameter T must extend Object which is incompatible with Foo

This is because we haven't set yet Foo's supertypes.  Of course this is
nonsense in the special case of Object.  I think we can fix the general
case by moving the subtype test later, probably to InlineCalls.
The tricky part is figuring out how to do the subtype test in InlineCalls.

Worst case, we can special-case Object, and change teh general case to warnings.

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?

No - we should probably have some variation of define-class that uses module syntax, rather than the special syntax that define[-simple]-class does.

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?

I suspect my patch may take care of that. -- --Per Bothner per@bothner.com http://per.bothner.com/

Attachment: ptype.patch1
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]