[Gambas-user] Bad logic on my part
Rolf-Werner Eilert
rwe-sse at osnanet.de
Thu Aug 16 13:19:54 CEST 2018
Am 16.08.2018 um 11:37 schrieb Tobias Boege:
> 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
>
Hm!
a = b And c
in my ears would sound like
If (a = b) And If (c = True) Then
At least I would interpret
If a Then
as
If a = True Then
(I use this with Boolean variables like "If IsPrinted Then" and also
with functions like "If Exist(path) Then")
In practice, I tend to avoid any brackets and And If concatenations and
use blocks and single-condition expressions:
If TypeOf(Data[13]) = gb.Integer Then
If Data[13] > 3 Then
.
End If
End If
This makes it clearer, at least for my brain...
Regards
Rolf
More information about the User
mailing list