LB Booster
Programming >> Liberty BASIC language >> Subtle difference in scope rules
http://lbb.conforums.com/index.cgi?board=lblang&action=display&num=1409608454

Subtle difference in scope rules
Post by Richard Russell on Sep 1st, 2014, 9:54pm

If you run this program you will get different results from LB 4.04 and LBB when the window is closed:

Code:
    call test
    wait

[label]
    close #w
    print "Jumped to the [label] *outside* the subroutine"
    wait

sub test
    open "Click the close button" for window as #w
    #w "trapclose [label]"
    exit sub

[label]
    close #w
    print "Jumped to the [label] *inside* the subroutine"
    wait
end sub 

LB 4.04 prints "Jumped to the [label] *outside* the subroutine" and LBB prints "Jumped to the [label] *inside* the subroutine".

LBB assumes that the relevant scope for the destination of the trapclose command is what is visible when the command is issued. On the other hand LB 4.04 appears to assume that the relevant scope is what is visible when the window is closed.

The Liberty BASIC docs have nothing to say on this issue. It seems to me that the way LBB behaves is more in keeping with the conventional understanding of 'scope', and it means the destination of the trapclose is determined at compile-time. With LB 4.04, however, the destination is determined at run-time and could actually vary depending on what the program happens to be doing when the window is closed, which strikes me as potentially problematic. However your mileage may vary.

Note that, in this specific case, you can make the program behave the same way on both LB and LBB simply by omitting the label inside the subroutine:

Code:
    call test
    wait

[label]
    close #w
    print "Jumped to the [label] *outside* the subroutine"
    wait

sub test
    open "Click the close button" for window as #w
    #w "trapclose [label]"
end sub 

Richard.
Re: Subtle difference in scope rules
Post by Rod on Sep 2nd, 2014, 6:02pm

But your sub comes across an exit sub and does not wait in the sub so the scope is back to the main program wait? So I would expect the lb4 result I would not expect LBB result.
Re: Subtle difference in scope rules
Post by Richard Russell on Sep 2nd, 2014, 7:34pm

on Sep 2nd, 2014, 6:02pm, Rod wrote:
But your sub comes across an exit sub and does not wait in the sub so the scope is back to the main program wait? So I would expect the lb4 result I would not expect LBB result.

There are two common applications of scope: 'lexical' (static) scope and 'dynamic' scope, as described in this Wikipedia article:

http://en.wikipedia.org/wiki/Scope%5F%28computer%5Fscience%29

Lexical scoping is the most common in modern programming languages: the scope is determined at compile time according to where in the source code the identifier is referenced. It is clear that, using this definition, the way LBB works is correct.

What you seem to be describing is 'dynamic scope', which is used by a few languages, notably Lisp and Perl, although the situation I described is not typical of the way in which dynamic scoping is generally applied (one function calling another).

Realistically it would be impractical for LBB to work any other way, and in my opinion it is highly desirable for the destination of the 'trapclose' to be determined at compile time (i.e. be predictable) rather than at run-time (and be potentially unpredictable). It also accords with the principle of 'information hiding'.

But a well-written program ought not to depend on this behaviour anyway. As I said, simply omitting the label within the subroutine makes the program work just the same in LB4 and LBB.

Richard.