[Development] New feature to qstring | QString::toBool()

Andre Somers andre at familiesomers.nl
Wed Oct 26 12:50:19 CEST 2011


Op 26-10-2011 10:02, Frans Klaver schreef:
> On Wed, Oct 26, 2011 at 9:21 AM, Andre Somers<andre at familiesomers.nl>  wrote:
>
>> IMHO, there is a big difference between a string that correctly converts
>> to false, and one that can not be converted to a boolean. Do you really
>> wish to make that difference invisible?
> Certainly not.
>
>> I agree with Jan Arve on this topic. You need to have verification that
>> a conversion succeeded.
> I do agree that you need to have the verification available, however,
> I daresay in a lot of cases the need for it is not that big. I at
> least don't care if or why a boolean conversion fails in a lot of
> cases (with QVariant currently).
OK, you might be right about that.
>
> Then there's readability for slightly more complex situations.
> Assuming that QString::toBool() acts like QVariant::toBool() and
> returns true if the contents are "1", "true", or "y", false otherwise,
> consider the following:
>
> bool ok;
> bool result = str.toBool(&ok);
> if (ok) {
>      if (result)
>          enableSuperFastRenderer();
>      else
>          disableSuperFastRenderer();
> } else {
>      doSomethingSmart();
> }
Note that the verification flag would be optional anyway. If you return 
false if ok is false, then you can easily choose if you need 
verification or not. It just needs to be clearly documented. However, if 
you think that it would be good for readability, wouldn't it then make 
sense to have a way to do this in a localized way as well? The various 
true & false strings could conceivably be in QLocale? Otherwise, you 
still end up in the same situation if your application needs 
localization (and most do, I think).

>
> What would you do if you couldn't parse string. Make the assumption
> you can, and enable the super fast renderer?
I *might* want to bail out and warn the user, but it depends on the 
situation, of course.

André




More information about the Development mailing list