[Development] QOptional

Poenitz Andre Andre.Poenitz at digia.com
Thu Aug 21 13:01:38 CEST 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. 
> - optional is much more friendly to static verification than &ok
(dependancy between variables is a mess to check).

Logically, in the presence of more than two alternatives, an argument 
against the current solution is not necessarily an argument in favour
of QOptional, let alone a 'strong' one.

The first "laziness" issue would e.g. also be addressed by the aforementioned
bool QString::isInt(int *value) setup which provides an obvious way to 
check success without giving the programmer the feeling to need to 
jump through hoops before being able to check for errors. 

I am actually not quite sure what you mean with the second item.

Something like the following?

struct S { int m_a, int m_b, int m_c);

// "Tradtional, wrong result"
bool S::parse(QString a, QString b, QString c)
{
    bool ok;
    m_a = a.toInt(&ok);
    m_b = b.toInt(&ok); // Oops, overwrites first ok value.
    m_c = c.toInt(&ok);
    return ok;
}

vs 

// "Tradtional, right result"
bool S::parse(QString a, QString b, QString c)
{
    bool oa, ob, oc;
    m_a = a.toInt(&oa);
    m_b = b.toInt(&ob); 
    m_c = c.toInt(&oc);
    return oa && ob && oc;
}

vs

// "Optional"
bool S::parse(QString a, QString b, QString c)
{
    auto oa = a.toInt();
    auto ob = b.toInt();
    auto oc = c.toInt();
    m_a = *oa;
    m_b = *ob;
    m_c = *oc;
    return oa && ob && oc;
}

vs 

// "isInt"
bool S::parse(QString a, QString b, QString c)
{
    bool oa = a.isInt(&m_a);
    bool ob = b.isInt(&m_b);
    bool oc = c.isInt(&m_c);
    return oa && ob && oc;
}

vs 

// "something else"
bool S::parse(QString a, QString b, QString c)
{
    return a.isInt(&m_a) && b.isInt(&m_b) && c.isInt(&m_c);
}


If so, the "Optional" variant doesn't look much simpler than any 
of the others, even less so when one doesn't accept the use of 
'auto' there.


> 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)

... and a show-stopper when it comes to using it with QString::toInt
(with exactly that 'toInt' function name)

Andre'



More information about the Development mailing list