Иван Комиссаров
21 авг. 2014 г., в 13:25, Simon Hausmann <[email protected]> написал(а):
> On Thursday 21. August 2014 13.13.15 Иван Комиссаров wrote:
>
> 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.
>
> I don't think that makes for a very intuitive API. Yes, it's less to type,
> but
> less isn't always more :)
Writing toInt() method itself becomes more easier too:
QOptional<int> toInt()
{
....
if (error)
return QNothing();
return value;
}
Compare with
int toInt(bool *ok = 0)
{
....
if (ok)
*ok = !error;
if (error)
return 0;
return value;
}
Also, using Optional/Maybe types can lead to a more functional-style code - you
can store error state in a member variable or return using bool as shown above
(which leads to extra code if (error) { ok = false; return T(); }) or you can
use maybe-type. I used this approach for parsing network messages - without
optionals i had to use a reentrant class that stored an error state, with
optionals i removed the entire class and replaced it with a pack of thread-safe
functions (taking QByteArray and returning Optional<QVariantList> where
QVariantList is parsed data (yes, empty list is valid))
_______________________________________________
Development mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/development