[Development] QOptional

Иван Комиссаров abbapoh at gmail.com
Wed Aug 20 21:03:21 CEST 2014


Иван Комиссаров

20 авг. 2014 г., в 22:49, Marc Mutz <marc.mutz at kdab.com> написал(а):

On Wednesday 20 August 2014 20:04:24 Иван Комиссаров wrote:

> static fromOptionalValue()
> (like fromValue())
> 
> Please don't add a templated ctor to QVariant. It currently doesn't have one, 
> and if you add that ctor, as implicit even, you may change the meaning of this 
> code:
> 
>    QVariant v(t);
> 
> where t, say, has an implicit conversion to int. The call might become 
> ambiguous.
> 
> Also, QVariant v = t; for any currently supported type decltype(t) means that 
> v _contains_ a t. In particular, if QVariant v = t compiles, then it is equal 
> (in type and content) to fromValue(t). With your proposal that's no longer 
> true. If t is a QOptional<T>, v would now contain a T, which is different from 
> decltype(t).

Agreed.

> toOptional_Value_() (to mimick value<T>().

Ok

> What QOptional should be consistent with is a T*, and std::optional, because 
> that's what it's replacing. Qt smart pointers have isNull(). Just mention the 
> simile in the docs, and no-one will look for isNull(). IIRC, std::optional 
> uses something way more esoteric, like is_engaged() or so (I'm not proposing 
> that).
> 
> With isValid() you will have the same problem. If isNull() checks !opt || 
> opt.value().isNull(), then it's hard to argue why isValid() shouĺd check 
> anything but opt && opt.value().isValid().
> 
> I don't find the QVariant::isNull behaviour any useful or intuitive. It's too 
> smart. You can always use v.value<T>().isNull() because value() will return a 
> default-constructed T if invalid.

I'm not sure QOptional should mimick T*, not QVariant, but ok.

> So, the i vote for following behaviour;
> Invalid QVariant (no type) <-> invalid QOptional of type T
> null QVariant (QString) <-> valid QOptional containing QString()
> non-null QVariant containig QString("") <-> valid QOptional containing
> QString("") non-null QVariant containing non-empty string <-> valid
> QOptional containing non-empty string

So, we construct null QOptional from invalid QVariant and  non-null QOptional from valid?


More information about the Development mailing list