[Development] Lack of base classes/interfaces? Q*, Q*F

Jason H jhihn at gmx.com
Tue Apr 18 15:35:02 CEST 2017

> Sent: Monday, April 17, 2017 at 6:01 PM
> From: "Kevin Kofler" <kevin.kofler at chello.at>
> To: development at qt-project.org
> Subject: Re: [Development] Lack of base classes/interfaces? Q*, Q*F
> Alejandro Exojo wrote:
> > You should really explain which kind of code did you change, because just
> > recently I adjusted code from using a key press event to a touch event,
> > and the point had to change from QPoint to QPointF, and the changes were
> > minimal. This wasn't explained to me, but I just found it natural. Also:
> > 
> > https://wiki.qt.io/API_Design_Principles#Static_Polymorphism
> And for the OP who is coming from Python: C++ templates can actually be used 
> to duck-type things *at compile time*, and thus get some compile-time 
> polymorphism without an inheritance relationship. So if foo is a template 
> <typename T>, then calling at(i) on an object of type T will call 
> QVector::at(int) in foo<QVector> and QList::at(int) in foo<QList>, simply 
> because of the name. (Trying to instantiate the template with a class that 
> does not have an at(int) method will of course fail to compile.)
> What will of course *NOT* work in C++ is to duck-type things *at runtime*. 
> You cannot pass a QList to foo<QVector>. Runtime polymorphism is (in 
> general) only possible through virtual methods and inheritance. (And yes, I 
> know about the possibilities to (ab)use QMetaObject for Q_OBJECTs or 
> Q_GADGETs to get there. That is out of scope here.)

Thank Kevin, yes compile-time is fine for my current engineering challenge. Even though I can in python, I tend to eschew runtime typing. I think the lack of compile-time type checks is a problem with building larger software systems (I have yet to see enterprise software written in JS (though TypeScript addresses this). My concern is mainly allowing the developer to chose the concrete implementation and having my code work with whatever implementation the developer has chosen (in the case of QVector vs QList of QRect vs QRectF). I think runtime type checks (isinstance() in python) are bad because it greatly increases the test cases.  

More information about the Development mailing list