Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Variable scope suggestion

  1. #1

    Question Variable scope suggestion

    Hi,

    Currently and according to the documentation, for a given variable there can be :

    • Local scope inside a sub or function,
    • Global scope inside a script,

    to the extent of my knowing and at this stage of thinBasic developpement, there can't be an Include scope
    which could be useful when splitting a script into reusable/shareable parts between projects
    and assuring there will be no variable-name conflict.

    Actual workaround is to manually prefix each variable to make distinction.

    Same idea for subs/functions ?


    Your opinion ?
    Last edited by DirectuX; 20-11-2018 at 09:13.
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  2. #2
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,529
    Rep Power
    170
    Maybe you could use Private scope if you try to stick to type-oriented programming.
    It's like
    Type t_TypeWithAPrivate
       Private
       mySubelement As Byte
       Public 
       anotherSubelement As Word
       '...
       ' Me.mySubelement is only accessible
       ' inside functions
       ' between Type - End Type...
    End Type
    Dim foo As t_TypeWithAPrivate
    ' foo.anotherSubelement is accessible from
    ' anywhere in the project
    
    If you create each include-file - including it's own variables and functions - as a Type then it's possible to use dot-notation. Always Me.varName within the code-unit (between Type and End Type) else prefix the unit with a fitting name (foo in the example), something like
    "SE" - sound-engine
    "BUG" - error-handler
    "DX" - your directX wrappers...
    Last edited by ReneMiner; 20-11-2018 at 22:53.
    I think there are missing some Forum-sections as beta-testing and support

  3. #3
    Hi,

    Yes, TYPE would reduce the name-conflict risk to one variable, let the functions.
    Goal is indeed to avoid name conflict. But I surely misunderstand this :

    Quote Originally Posted by ReneMiner View Post
    If you create each include-file - including it's own variables and functions - as a Type
    Do you mean wrap TYPE / END TYPE around the whole included code ?

    Quote Originally Posted by ReneMiner View Post
    else prefix the unit with a fitting name (foo in the example), something like
    "SE" - sound-engine
    "BUG" - error-handler
    That's how I started but it's heavy, especially since thinAir's autocomplete doesn't suggest user defined variables or functions.
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  4. #4
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,529
    Rep Power
    170
    Yes of course Type-End Type around the whole code-unit. The only thing outside Type-End Type would be for example:

    Dim DX As t_DirectX


    Hmm, the auto-complete... Yes...

    Eros !!?

    How about reviving the tbProject-file-extension ???
    Could be easier if a tbProject-file contains all
    -global external function-declarations,
    -included code-units (a global variable for each)
    -global dimensioning
    -Function TBMain()

    Built-in to the tbProject-file should be directives:

    Option Explicit
    No untyped subs nor functions outside the tbProject-file
    No global variable-dimensioning nor external function-declarations (.DLL) nor #include-statements in any other code-unit than in the TBPROJECT-FILE.
    All additional code-units only persist of Type-definitions
    A code-unit will be included in a way like

    #Include Global DX As "myDirectX.tBasicU"

    The first codeline of myDirectX.tBasicU must start with
    Type ...(no matter the name, Extends invalid here)
    and ends with
    End Type

    anything below End Type in this file will be ignored.

    ...

    Better may be
    Alias Type As CodeUnit
    In the example:

    CodeUnit myDirectX
    ' this is my directX-wrapper
    ...
    End CodeUnit

    So it's possible to define other Types here and the code above CodeUnit or below End CodeUnit can hold UDT-definitions.

    Declarations of Functions from external libraries within CodeUnit and End CodeUnit could be possible... As udt-subelements!
    Those were local to the CodeUnit.

    Example ( I return to Type-syntax for better understanding)
    Type myDirectX
      Declare Function XXX Alias "XXX_ex" Library "dxlib.DLL"(parameters) As Long
    End Type
    
    Dim DX As myDirectX
    Long foo = DX.XXX(parameters)
    
    Perhaps with rules and directives like that it will be possible to maintain backward-compatibility and make a step into direction TOP (type-oriented-programming)
    as well as having auto-complete for udt-variables

    On the other hand, is there a code-browser as in the old thinAir that lists properties (subelements & functions) of udts?
    Last edited by ReneMiner; 21-11-2018 at 00:50.
    I think there are missing some Forum-sections as beta-testing and support

  5. #5
    Quote Originally Posted by ReneMiner View Post
    Yes of course Type-End Type around the whole code-unit. The only thing outside Type-End Type would be for example:
    Dim DX As t_DirectX
    That! It is something different. I wouldn't never guess that MemberName could represent a function.

    TYPE

    Description
    Define a User-Defined Data Type (UDT), containing one or more member elements.

    Syntax

    TYPE MyType [BYTE | WORD | DWORD]


    [STATIC] [MemberName AS TypeName]

    [STATIC] [MemberArrayName[(nElements)] AS TypeName]

    [...]

    END TYPE

    source: https://www.thinbasic.com/public/pro.../html/type.htm


    Quote Originally Posted by ReneMiner View Post
    Could be easier if a tbProject-file contains all [...]
    Except this wouldn't answer the original difficulty. You couldn't have an autosufficent-include this way, or I missed something.
    It's a different feature, interesting, but different.

    I would add about that, that there is no need to have a separate file, to keep it simple, one can make use of something like #Region / #EndRegion.

    Still, the user definitions recognition (equates, functions with param, vars) is, to my eyes, a must for thinAir.
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  6. #6
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,529
    Rep Power
    170
    I suggest this thread
    https://www.thinbasic.com/community/...pes-micro-book
    to learn more about the fantastic abilities of user defined types in thinBasic
    I think there are missing some Forum-sections as beta-testing and support

  7. #7

    I already had a glance at Petr's UDT book but missed the §1.4.2 UDT Function. Thanks to point that out to me.
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  8. #8
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,529
    Rep Power
    170
    Auto-complete of thinAir should be improved then.
    I know if all of the source-code has to be scanned in real time it will make the handling/typing slow and laggy.
    That's why I thought the tbProject-file containing all global dimensioned/declared stuff it would need to "refresh" those internally only when the input-focus changes to another CodeUnit or if the user explicitly clicks on a refresh-button.
    To scan local if cursor within a function then it will be fast enough in real time - I tried already, I know that -someone might remember my thinICE-project that kicked an avalanche off the tB-hills

    Some code-overview (tree) that shows everything in a structured way is a very nice tool - not only for the user to look at but also to pick expressions that should show up in auto-complete


    If cursor within a type-function it's still OK to scan from Type to End Type, if outside a type-function but between type and end type it's obsolete to scan since the Type-definition is currently about to be changed...
    Last edited by ReneMiner; 21-11-2018 at 21:19.
    I think there are missing some Forum-sections as beta-testing and support

  9. #9
    Quote Originally Posted by ReneMiner View Post
    someone might remember my thinICE-project
    Seen with screenshots on the forum.

    Quote Originally Posted by ReneMiner View Post
    I know if all of the source-code has to be scanned in real time it will make the handling/typing slow and laggy.
    That's why I thought the tbProject-file containing all global dimensioned/declared stuff it would need to "refresh" those internally only when the input-focus changes to another CodeUnit or if the user explicitly clicks on a refresh-button.
    To scan local if cursor within a function then it will be fast enough in real time
    It is sufficient to
    • scan the line of code that was changed, relevant event is cursor's line-change.
    • scan any pasted piece of code (rare lag may be acceptable here)

    Of course, there is a full scan at loading.

    Quote Originally Posted by ReneMiner View Post
    but between type and end type it's obsolete to scan since the Type-definition is currently about to be changed...
    What ?
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  10. #10
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,529
    Rep Power
    170
    Example
    Type tType
    
       ' if cursor here no need to scan this udt
       ' because it's about to be changed
      Function myfunc()
        ' here this udt and the variables/parameters of 
        ' this function have to be scanned until scan 
        ' finds keyword End or another Function or
       ' unknown As vartype
    
    I think there are missing some Forum-sections as beta-testing and support

Page 1 of 3 123 LastLast

Similar Threads

  1. Suggestion: new for commands
    By kryton9 in forum Suggestions/Ideas discussions
    Replies: 7
    Last Post: 16-05-2017, 19:15
  2. One suggestion
    By Petr Schreiber in forum MSVC++ 6.0
    Replies: 3
    Last Post: 13-01-2006, 13:51

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •