[Gambas-user] What's happening with True and False string conversions?
tobs at taboege.de
Sat May 29 09:07:49 CEST 2021
On Fri, 28 May 2021, John Anderson wrote:
> Screenshot, system info, simple demo code and project are attached.
> > What the heck? Am I driving off into the weeds here?
> > Thanks
> > -John
> You must be careful with the functions that use localization and those
> who do not.
> Str(), Val() and Print use localization.
> CStr() and automatic conversion do not.
> Thanks Benoît and BruceSteers!
> I guess what is still a mystery is why does automatic conversion Print
> (expression) work perfectly fine when it's the only conversion on the
> line, but won't work if you've got more than one automatic conversion in
> one print statement? That's what had me really confused.
Print takes an expression, not just a string. It then prints its Str()
value. So `Print True` yields Str(True) which is "True". The reasoning
is that when you Print to console, you usually want localized output[*].
When you `Print True & False`, then True and False are subject to auto-
matic conversion under the & operator for strings. Automatic conversion
uses the low-level CStr() routine.
If you want to print multiple values to console using Print, you can
either explicitly convert them to strings using your preferred method,
or you can use the special ,; separators in Print statements:
Print True; False ' "TrueFalse"
Print True;; False ' "True False"
Print True, False ' "True\tFalse"
These operators insert nothing, a space or a tab between expressions
and print each expression using Str(), as Print normally does.
Think of ; as a Print-specific operator which does & but whose conversion
is Str() instead of CStr().
[*] The flip side of this is that you must be careful when your output
is intended for machines. Printing a Float is subject to localization
as well and the decimal separator depends on the locale under which
your program runs. In this case you should explicitly `Print CStr(float)`
to get the period as decimal separator, which seems to be the standard
notation and is most likely to be understood by the receiving end.
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
More information about the User