[Development] RFC: more liberal 'auto' rules?

Rafael Roquetto rafael.roquetto at kdab.com
Fri Dec 4 14:12:00 CET 2015


On Fri, Dec 04, 2015 at 02:25:14PM +0100, Olivier Goffart wrote:
> On Friday 4. December 2015 14:11:48 Oswald Buddenhagen wrote:
> > On Fri, Dec 04, 2015 at 02:07:10PM +0100, Marc Mutz wrote:
> > > And as an aside, since it has been mentioned in this thread: in Python
> > > _all_ variables are 'auto'. All. Without exception. Are Python
> > > programmers more intelligent? Or do they just tolerate more pain? :)
> > 
> > i'd suggest the latter.
> > no, really. people use external static checkers because the language
> > lacks the feature.
> > the lack of static typing is a common feature of scripting languages and
> > makes them convenient to a degree, but it is an utter nightmare for any
> > "real" software development. i really wouldn't want to go there.
> 
> But auto is still staticaly typed.
> 
> 
> When you have code like
> 
>    foo->bar()->setFaz(m_factory->createFaz(foo->bar()->type()));
> 
> You don't see any type. 
> 
> This code that use auto is not less readable. Even if you don't know what's 
> the type of bar without looking it up.
> 
>   auto *bar = foo->bar();
>   bar->setFaz(m_factory->createFaz(bar->type()));

Until you need to change your code and call any method of bar. Suppose I
need to change the following code, to print the first item of the collection:

    auto *bar = foo->collection();

    /*TODO: print first item here */

    model->setCollection(bar); // we know bar is a collection, but of what kind?


Choose the right alternative:

    a) qDebug() << bar->first();
    b) qDebug() << bar->values().first();
    c) qDebug() << bar[0]
    d) none of the above


To answer this, you will need to find out the return type of
Foo::collection(); The compiler knows it, but the reader does not. And the
answer depends on whether it is  a QList/QVector, QHash/QMap, etc... Had it
been explicit, any programmer familiar with the Qt API (or any API in context)
would be able to directly write down the right statement.


Just an idea.

Cheers,
Rafael

-- 
Rafael Roquetto | rafael.roquetto at kdab.com | Software Engineer
Klarälvdalens Datakonsult AB, a KDAB Group company
Tel. Sweden (HQ) +46-563-540090, USA +1-866-777-KDAB(5322)
KDAB - Qt Experts
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 4861 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20151204/c00c0458/attachment.bin>


More information about the Development mailing list