[Kde-bindings] Adding more explicit ambiguous method lookup resolution for PerlQt

Richard Dale rdale at foton.es
Wed Jun 10 15:34:08 UTC 2009


On Wednesday 10 June 2009 06:28:33 am Chris Burel wrote:
> Hey guys,
> Sorry for double posting, but I wanted to get Ruby's opinion on this too.
>
> One thing in PerlQt that can be very frustrating is when the bindings
> cannot determine which method you intend to call based on the
> arguments.  A common example of this happens with the QVariant class,
> because it's essentially a union class that has constructors for a
> bunch of different data types.  Consider:
> my $foo = Qt::Variant(1)
> results in
>
> --- Ambiguous method QVariant::QVariant called
> Candidates are:
>         QVariant::QVariant( uint )
>         QVariant::QVariant( int )
>         QVariant::QVariant( bool )
> Choosing first one...
>
> It's frustrating because the bindings clearly know that the other
> method exists, and sometimes you really do want to call the other
> method.  But how should you specify which method you really want?
>
> I was thinking that if we already have the method's signature, and it
> is unique, why not let the coder specify the exact signature they want
> to call?  Something along the lines of
> Qt::setSignature( 'QVariant::QVariant( bool )' );
> my $foo = Qt::Variant(1);
> That way the coder can force the bindings to call the correct method.
In QtRuby you can pass an optional second argument to the 
Qt::Variant#fromValue method to indicate the exact type you want:

mardigras rdale 657% irb -rQt4
irb(main):001:0> v = Qt::Variant.fromValue(3)
=> #<Qt::Variant:0xb6276490 typeName=int>
irb(main):002:0> v = Qt::Variant.fromValue(3, "int")
=> #<Qt::Variant:0xb6271918 typeName=int>
irb(main):003:0> v = Qt::Variant.fromValue(3, "uint")
=> #<Qt::Variant:0xb626e560 typeName=uint>


> It can really help for things like the QSignalMapper, that has these 2
> functions:
> void setMapping ( QObject * sender, QWidget * widget )
> void setMapping ( QObject * sender, QObject * object )
> Because of the ambiguity, the coder doesn't know if she/he should
> connect to the 'mapped( QWidget * widget )' signal or the 'mapped(
> QObject * object )' signal.
There are few enough of these that they can be special cased - that's what 
QtRuby does for the above one. They are implemented as C methods which look at 
the type of the second argument and call the correct method, rather than going 
through the usual method_missing() (ie autoload in perl) mechanism.

-- Richard



More information about the Kde-bindings mailing list