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

Andreas Aardal Hanssen andreas at hanssen.name
Thu Oct 27 20:13:25 CEST 2011


2011/10/26 Christoph Feck <christoph at maxiom.de>
> As far as I remember, POSIX locales offer strings or regexps for YES
> and NO, so it might be possible to add something to QLocale to access
> those strings.

By auto-converting a QString to a bool we'll be adding ambiguity to Qt
APIs. If there's no intuitive way to convert a QString to a boolean,
then QString IMO should not provide that as a function... I find it
especially disturbing that translatability and locale is considered.
Literal interpretation of a string needs to be completely unambiguous,
just like QString("123").toInt() is.

If we add QString::toBool(), I think we are adding future bugs to
applications that make use of this function. We'll find code that
calls toBool() on all kinds of strings with hard-to-read outcomes.

QString(QChar::Null).toBool()
QString("0").toBool()
QString("null").toBool()
QString("nil").toBool()
QString().toBool()
QString("false").toBool()
QString("no").toBool()
QString("not").toBool()
QString("!").toBool()
QString("negative").toBool()

Do any of these examples have any _intuitive_, unambiguous return values?

I vote -1 on this feature. Just write:

if (value == "true")
{
}

or

if (value.compare("true", Qt::CaseInsensitive) == 0)
{
}

or write a grammar and a proper parser :-).

--
Andreas Aardal Hanssen



More information about the Development mailing list