[Development] QOptional
Albert Astals Cid
albert.astals at canonical.com
Thu Aug 21 12:02:09 CEST 2014
On Thursday 21 August 2014 09:50:04 Poenitz Andre wrote:
> Hausmann Simon wrote:
> > On Thursday 21. August 2014 13.13.15 Иван Комиссаров wrote:
> > > Of course, i can:)
> > >
> > > bool ok;
> > > const int value = string.toInt(&ok);
> > > if (ok)
> > >
> > > qDebug() << "value is" << value;
> > >
> > > // ====
> > > const auto value = string.toInt();
> > > if (value)
> > >
> > > qDebug() << "value is" << *value;
> >
> > To be honest: I find the "bool ok" variant much easier to read.
> > "if (value)" on an auto variable can mean so many things. In the
> > above example you could very easily think: Ah, toInt returns an int,
> > so the type of "value" is probably int, so if (value) checks if it's
> > non-zero.
> So the "ugliness" of the "traditional" code is essentially hidden
> by the use of 'auto' in contexts where the type is not obvious
> to the casual reader.
>
> Another can of worms. In practice this would end up often
> enough with
>
> bool ok;
> const int value = string.toInt(&ok);
> if (ok)
> qDebug() << "value is" << value;
>
> vs
>
> const QOptional<int> value = string.toInt();
> if (value)
> qDebug() << "value is" << *value;
>
> which is as cluttered as before in my eyes.
+1
Cheers,
Albert
More information about the Development
mailing list