On Thu, 22 May 2008, Bill Janssen wrote:


On Thu, 22 May 2008, Bill Janssen wrote:

I'm trying to figure out an issue here which sounds sort of similar.

I've got a class in UpLib:

  class UpLibQueryParser (PythonMultiFieldQueryParser):

in one of the methods (getFieldQuery), it needs to reparse the input,
so I create a new instance of my class:

        newparser = self.__class__(self.fields, self.getAnalyzer())

and then cast it to a QueryParser so that I can call "parse()" on it:

        qp = QueryParser.cast_(newparser)

But I get an exception at this point:

 File "/local/share/UpLib-1.7.4/code/uplib/indexing.py", line 320, in 
getFieldQuery
   qp = QueryParser.cast_(newparser)
TypeError: <UpLibQueryParser: [EMAIL PROTECTED]>

Above, I'm using "self.__class__" because I also create subclasses of
this class, and I want an instance of the most specific subclass
created.

How about using type(self)(...) instead ?

Andi..

Well, I print out the instance that's created, and it is the right
class, so I think there is a bug in the cast_ mechanism (maybe
PythonMultiFieldQueryParser doesn't know it inherits from QueryParser?
After all, one is a Python class, and the other a Java class).  But I
tried using "type(self)" as you suggest, and I get the same TypeError.

The point of cast_() is to make it possible to downcast, say, from Object to Field. Upcasting is not going to achieve anything as the underlying Java object remains the same (either way) and the wrapper is calling the same methods. When downcasting, the new wrapper now has a bunch of methods that know how to talk to instances of that class.

In other words, if you upcast a MultiFieldQueryParser to a QueryParser, were this to work (I don't know right now why that fails), it'd be a nop. At best you'd lost some methods. But the methods you'd keep, like parse() would still call the same code on MultiFieldQueryParser.

Andi..

_______________________________________________
pylucene-dev mailing list
pylucene-dev@osafoundation.org
http://lists.osafoundation.org/mailman/listinfo/pylucene-dev

Reply via email to