[Gambas-user] Bad logic on my part
Tobias Boege
taboege at gmail.com
Thu Aug 16 11:37:59 CEST 2018
On Thu, 16 Aug 2018, Rolf-Werner Eilert wrote:
> Am 16.08.2018 um 07:46 schrieb Me:
> > What the *@^^& have I got wrong here?
> >
> > Dim Data As Variant[]
> > If (TypeOf(Data[13]) = gb.Integer And If Data[13] > 3) Then ...
> >
> > gives a "Missing ")" in class ..." message at the compile time.
> >
> > Data[13] is either an integer or a string.
> >
> > (Leave town for 30 seconds and they change everything.)
> >
> > tia
> > b
> >
>
> If using brackets, shouldn't it be something like
>
> If (TypeOf(Data[13]) = gb.Integer) And If (Data[13] > 3) Then...
>
> However, I would have used
>
> If TypeOf(Data[13]) = gb.Integer And Data[13] > 3 Then...
>
> Is there really a difference?
>
I'm never sure about that. I always use "And If" because it has the loosest
precedence (and short-circuits). Under some circumstances when you use just
And, the compiler might interpret it as a bitwise And instead of the logical
one. I guess if you use "a = b And c > d", this is unambiguous because
operators don't chain, but consider "a = b And c". You'd think "*of course*,
this should be read as 'a = (b And c)' instead of '(a = b) And c'", but it
isn't that obvious anymore here:
If iLines = iTotalLines And iFound Then Break
That is, if, like me, you are used to writing "iFound" as a Boolean condition
to mean "iFound <> 0".
And yes, the error seems to come from the And If inside a pair of parentheses,
which block its view to the corresponding If.
Regards,
Tobi
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
More information about the User
mailing list