Difference in WAIT when in a function

Discussions relevant to the differences between LBB and LB4 which could affect code compatibility
Post Reply
tsh73
Posts: 22
Joined: Fri Apr 06, 2018 7:58 pm

Difference in WAIT when in a function

Post by tsh73 » Fri Apr 13, 2018 6:39 pm

Trying to debug code from Formatting Numeric Input thread,
I found that program in LB breaks after handling first event
while in LBB it works OK.
The problem code was function with WAIT inside
Here's the code I came up with.

My guess that in JB/LB
after WAIT program waits on the spot it happened.
That is, inside function.
So no labels are visible that is outside this function (but label inside it are visible - button 1 works, with new label)

But in LBB it looks like always waits on top-level
so on first button, first label always used.
- this is my guess, it might be wrong.
I hope Richard could tell ;)

Now, it is just difference one better be aware of.

Code: Select all

'   Form created with the help of Freeform-J v.261006
'   Generated on Apr 13, 2018 at 13:47:19

    nomainwin

    WindowWidth = 344
    WindowHeight = 220

    UpperLeftX=int((DisplayWidth-WindowWidth)/2)
    UpperLeftY=int((DisplayHeight-WindowHeight)/2)

    statictext #main.statictext1, "Press button 1. It will cal WAIT in a function. Now try any button/trapclose. Probably all labels will be unreachable.", 26, 16, 288, 50
    statictext #main.statictext1, "(you better close with KillBasic Progam from JB IDE)", 26, 70, 288, 80
    button #main.button1, "Button1", [button1Click], UL, 30, 141, 122, 25
    button #main.button2, "Button2", [button2Click], UL, 190, 141, 122, 25
    open "wait in  function - test" for window as #main
    print #main, "trapclose [quit.main]"

    print #main, "font ms_sans_serif 10"

    wait

[quit.main]
    Close #main
    END


[button1Click]    'Perform action for the button named 'button2'
    notice "Button 1 clicked"
    dummy = someFunction()
    wait


[button2Click]    'Perform action for the button named 'button3'
    'Insert your own code here
    notice "Button 2 clicked"
    wait

function someFunction()
    notice "We are in a function"
    wait    'this one probably should't be done
[button1Click]
    notice "this label1 is available in a function"
    wait
end function

guest
Site Admin
Posts: 56
Joined: Tue Apr 03, 2018 1:34 pm

Re: Difference in WAIT when in a function

Post by guest » Fri Apr 13, 2018 7:50 pm

tsh73 wrote:
Fri Apr 13, 2018 6:39 pm
I hope Richard could tell ;)
Without studying it, my guess is that the behavior arises because of the different 'scoping' rules of LB 4 and LBB, that is, from where in the program a particular branch label is visible. In LB 4 the 'isolation' provided by a function/sub works in both directions: that is a branch label inside a function is not visible from outside, and a branch label outside a function is not visible from inside. LBB, however, has 'hierarchical' scoping rules: a branch label inside a function is not visible from outside but a branch label outside a function MAY BE visible from inside.

Hierarchical scoping is common in other languages, for example in C; one advantage is that in the event of a 'fatal' (unrecoverable) error occurring inside a sub/function, the program can successfully jump to a global error handler; in LB 4 it can be difficult to deal with errors of this kind. However the disadvantage is that it can make a program appear to be working correctly even if it has a serious fault; in particular if the program has a WAIT inside a function, and an event occurs the handler for which is outside the function, the program may seem to work even though there is a memory and/or data leak.

Richard.

Post Reply