JCC handling templated types yet?
Does JCC support templated types yet? In particular, I'd like to be able to say, --sequence List 'size:()I' 'get:(I)Lcom/parc.goodstuff.Stuff;' Bill
Re: JCC handling templated types yet?
On Apr 3, 2010, at 11:38, Bill Janssen wrote: Does JCC support templated types yet? Yes, Lucene Java moved to Java 5 and makes extensive use of generics in the 3.x release series. I added support for that in JCC 2.5. In particular, I'd like to be able to say, --sequence List 'size:()I' 'get:(I)Lcom/parc.goodstuff.Stuff;' The first arg to --sequence is that of an existing class, and the following, the actual signatures of existing methods on it. If you define a class foo parameterized on type T and that type is the return type of a method, that type is used, at runtime, as the wrapper for the return value. JCC does not generate code for all known expansions of a generic class - that would lead to serious code explosion. Instead, the python instance of an instatiated parameterized java class contains the type parameters used and the wrapper methods use them to produce python return objects or accept python parameters of the expected types. The support for generics is good enough for Lucene's usage - not bad:-) - but may have some holes and bugs still. I recently fixed a bug on trunk with failing to inherit type parameters to inner classes, for example. Also, there doesn't seem to be a way, from python, to directly instantiate a class passing it type parameters. There is from C++, of course. Consider that an oversight. I should add support for that. If you'd like to see the parameters of an instance of a class you know is parameterized, use the parameters_ property. Andi.. Bill
Re: JCC handling templated types yet?
On Sat, 3 Apr 2010, Andi Vajda wrote: On Apr 3, 2010, at 11:38, Bill Janssen wrote: Does JCC support templated types yet? Yes, Lucene Java moved to Java 5 and makes extensive use of generics in the 3.x release series. I added support for that in JCC 2.5. In particular, I'd like to be able to say, --sequence List 'size:()I' 'get:(I)Lcom/parc.goodstuff.Stuff;' The first arg to --sequence is that of an existing class, and the following, the actual signatures of existing methods on it. If you define a class foo parameterized on type T and that type is the return type of a method, that type is used, at runtime, as the wrapper for the return value. JCC does not generate code for all known expansions of a generic class - that would lead to serious code explosion. Instead, the python instance of an instatiated parameterized java class contains the type parameters used and the wrapper methods use them to produce python return objects or accept python parameters of the expected types. The support for generics is good enough for Lucene's usage - not bad:-) - but may have some holes and bugs still. I recently fixed a bug on trunk with failing to inherit type parameters to inner classes, for example. Also, there doesn't seem to be a way, from python, to directly instantiate a class passing it type parameters. There is from C++, of course. Consider that an oversight. I should add support for that. I added support for a new method on classes that can be parameterized called 'of_'. It takes one or more wrapper class parameters, corresponding to the parameters the java class was declared with. For example: >>> a=ArrayList().of_(String) # instantiate an ArrayList >>> a.add('foo') True >>> a.get(0) >>> a.of_(None) # reset its type parameter to nothing >>> a.get(0) >>> a.of_(Double) # mess its type parameters up >>> a.add(1.5) True >>> a.get(0) Traceback (most recent call last): File "", line 1, in TypeError: >>> a.get(1) >>> This is checked into jcc's trunk. Andi..
Re: JCC handling templated types yet?
Andi Vajda wrote: > On Apr 3, 2010, at 11:38, Bill Janssen wrote: > > > Does JCC support templated types yet? > > Yes, Lucene Java moved to Java 5 and makes extensive use of generics > in the 3.x release series. > I added support for that in JCC 2.5. > > > In particular, I'd like to be able to say, > > > > --sequence List > > 'size:()I' > > 'get:(I)Lcom/parc.goodstuff.Stuff;' > > The first arg to --sequence is that of an existing class, and the > following, the actual signatures of existing methods on it. Let's see if I understand this. I'm not sure I know what the "existing classes" are. In this case, the type is java.util.List. java.util.List is an interface rather than a class. On the other hand, java.util.List is a parameterized type of some sort. A parameterized interface? I see no .class file for it. So I'm guessing I can't do what I want to do. > If you define a class foo parameterized on type T and that type is the > return type of a method, that type is used, at runtime, as the wrapper > for the return value. I'm sorry, but I don't think I understand what you're saying here. Which type is the return type of a method, "foo" or "T"? > JCC does not generate code for all known expansions of a generic class > - that would lead to serious code explosion. I assume this also includes generic interface types. How about generating code for expansions that are specifically called out in the args to JCC, as in the example above? Bill
Re: JCC handling templated types yet?
On Sat, 3 Apr 2010, Bill Janssen wrote: Andi Vajda wrote: On Apr 3, 2010, at 11:38, Bill Janssen wrote: Does JCC support templated types yet? Yes, Lucene Java moved to Java 5 and makes extensive use of generics in the 3.x release series. I added support for that in JCC 2.5. In particular, I'd like to be able to say, --sequence List 'size:()I' 'get:(I)Lcom/parc.goodstuff.Stuff;' The first arg to --sequence is that of an existing class, and the following, the actual signatures of existing methods on it. Let's see if I understand this. I'm not sure I know what the "existing classes" are. In this case, the type is java.util.List. java.util.List is an interface rather than a class. On the other hand, java.util.List is a parameterized type of some sort. A parameterized interface? I see no .class file for it. There is a .class file for java.util.List so that's what I mean by 'existing'. The parameterizations of it are a runtime feature, not a compile time feature as in C++. So I'm guessing I can't do what I want to do. Yes, you could, were it not for the bug that, currently, --sequence code does not heed parameterization. I'm fixing this next. So, assuming this bug were fixed, using: --sequence java.util.AbstractList 'size:()I' 'get:(I)Ljava/lang/Object;' Note: you can't use List since, in Python, ArrayList doesn't inherit from List because a Python C type can have only one base and that's AbstractList <- AbstractCollection <- Object. Hence, use AbstractList instead. Then, in your code: >>> list = ArrayList().of_(Stuff) >>> list.add(Stuff()) >>> list[0] If you define a class foo parameterized on type T and that type is the return type of a method, that type is used, at runtime, as the wrapper for the return value. I'm sorry, but I don't think I understand what you're saying here. Which type is the return type of a method, "foo" or "T"? say you have: class Foo { T get(int i) { return something; } } then when you do elsewhere in java: Foo foo = new Foo(...); Stuff fromFoo = foo.get(0); //get returns T, bound to Stuff in Foo In Python, by way of JCC, that T bound to Stuff is visible in the foo instances's parameters_ and is set via calls compiled to return it or manually via the new of_() method. >>> foo = Foo(...).of_(Stuff) >>> fromFoo = foo.get(0) JCC does not generate code for all known expansions of a generic class - that would lead to serious code explosion. I assume this also includes generic interface types. How about generating code for expansions that are specifically called out in the args to JCC, as in the example above? That is not necessary. It's all the same code, even in Java. Andi..
Re: JCC handling templated types yet?
On Sat, 3 Apr 2010, Andi Vajda wrote: Yes, you could, were it not for the bug that, currently, --sequence code does not heed parameterization. I'm fixing this next. So, assuming this bug were fixed, using: --sequence java.util.AbstractList 'size:()I' 'get:(I)Ljava/lang/Object;' Note: you can't use List since, in Python, ArrayList doesn't inherit from List because a Python C type can have only one base and that's AbstractList <- AbstractCollection <- Object. Hence, use AbstractList instead. Then, in your code: >>> list = ArrayList().of_(Stuff) >>> list.add(Stuff()) >>> list[0] It's now fixed in trunk: >>> a = ArrayList().of_(String) >>> a.add('foo') >>> a.add('bar') >>> a[0] >>> a[1] Andi..