[Development] QOptional
Harri Porten
porten at froglogic.com
Thu Aug 21 12:30:19 CEST 2014
On Thu, 21 Aug 2014, Julien Blanc wrote:
> There are two strong arguments in favor of optional :
> - real life tends to show that the optional &ok parameter is often
> missed in context it should not, leading to hard to find bugs. People
> just write
>
> const int value= string.toInt();
>
> optional makes it more obvious for the lazy user that this can fail in
> some contexts.
Unfortunately, I have some doubts about that :}
> It can also assert in debug if dereferenced, for example,
> making bug finding easier.
>
> - optional is much more friendly to static verification than &ok
> (dependancy between variables is a mess to check).
>
> Still, the issue about the semantic of “if(value)” when value == 0 /
> false is still a concern (and one of the reason optional is not part of
> the standard iirc)
Others have said that it's too late to change toInt() and QSql. But how
about new code including that written by application developers? I
personally found myself repeating an implementation of "is valid" in
custom types to signal lack of a proper result. To avoid this repetition
(which uglifies the type implementation) I can see QOptional being useful.
Harri.
More information about the Development
mailing list