LB Booster
« Subtle difference in scope rules »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:46am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Subtle difference in scope rules  (Read 670 times)
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Subtle difference in scope rules
« Thread started 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.
« Last Edit: Sep 1st, 2014, 10:17pm by Richard Russell » User IP Logged

Rod
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 110
xx Re: Subtle difference in scope rules
« Reply #1 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.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Subtle difference in scope rules
« Reply #2 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.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls