Announcement

Collapse
No announcement yet.

Twice LOCAL

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Twice LOCAL

    Yep, still wondering about stuff.

    I vaguely thought that a compiler ought to allow redefining the Type of a variable on demand, as in this next code. Not that it would be good practice, but because it falls in the category of "But, I want to!".

    Code:
    Sub Test
       Local x as Long
       x = 5
       Local x as String
       x = "Tada"
    End Sub
    With that limit in mind, I found it interesting that this does not fail during compilation nor at run time. Compile time I would understand. But I thought it should fail at run time.

    Code:
    #Debug Display On
    #Debug Error On
    Function PBMain() As Long
       Local i As Long
       For i =1 To 5
          Local x As Long
          x =5
       Next i
       ? "Bingo"
    End Function

  • #2
    x is declared before first reference to it. And LOCAL does not exist at run time; it is only there to tell the compiler the type.

    Now to preference - some folks will LOCAL x AS LONG, y AS STRING '(one line). I only put one type on a line. Sometimes if I think the variable name needs explanation then only one variable on a line plus comment. Uses a lot of LOCALs, but they're free!
    Dale

    Comment


    • #3
      If one uses DIM ALL the following compiler error is produced:

      Error 466 in C:\PBWin10\Programs\spherefr.bas(25:010): This name is already in use: X
      Line 25: LOCAL x AS STRING
      Rod
      "To every unsung hero in the universe
      To those who roam the skies and those who roam the earth
      To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

      Comment


      • #4
        Compiles and runs fine here -
        Code:
        #compile exe
        #dim all
        function test() as long
          local x as long, y as string
          x=1
          y="1"
          function = x
        end function
        function pbmain () as long
          ? str$(test)
        end function
        ?? what did I miss?

        P.S. did you miss comma in my previous post?
        Dale

        Comment


        • #5
          deleted, to avoid confusion
          "Not my circus, not my monkeys."

          Comment


          • #6
            Eric, did you try that?
            Code:
            #compile exe
            #dim all
            function test() as long
              x=1
              y="1"
              function = x
              local x as long, y as string '<== moved from top of function
            end function
            function pbmain () as long
              ? str$(test)
            end function
            Moved LOCAL as noted, now I'm getting "Missing Declaration: x" upon compile.

            Cheers,
            Dale

            Comment


            • #7
              OOPS, no I didn't. I would have sworn that worked! I'll play around with it. I've removed my post from 30 minutes ago.
              "Not my circus, not my monkeys."

              Comment


              • #8
                ((could have just edited that part out ))
                Dale

                Comment


                • #9
                  I always declare all my data types before the PB Main
                  I line them up carefully as a nice lined up and tabbed list is one way of checking consistency
                  With some of my programs with huge data lists, I put the data lists in an #INCLUDE or sometimes more than one #INCLUDE
                  I wish PB would flag any fields that i do not use - that would be very helpful

                  Kerry
                  [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
                  Kerry Farmer

                  Comment


                  • #10
                    ??? LOCAL variable declarations must be in the procedure (function or sub) where they are used. Are you talking about globals and/or equates/constants?
                    Dale

                    Comment


                    • #11
                      Originally posted by Dale Yarker View Post
                      ??? LOCAL variable declarations must be in the procedure (function or sub) where they are used. Are you talking about globals and/or equates/constants?
                      yes - achieves the same end?????
                      [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
                      Kerry Farmer

                      Comment


                      • #12
                        You never use LOCAL?
                        Dale

                        Comment


                        • #13
                          This compiles to the default local, but doesn't reuse the same variable.

                          Code:
                          FUNCTION PBMAIN () AS LONG
                           x$ = "tada"
                           x& = 99
                          END FUNCTION
                          https://www.tesla.com/roadster

                          Comment


                          • #14
                            Originally posted by Dale Yarker View Post
                            You never use LOCAL?
                            No

                            I like to keep the algorithm aspects clear and obvious and minimal
                            [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
                            Kerry Farmer

                            Comment


                            • #15
                              Mike,

                              Not having #DIM ALL allows lots of things to happen.
                              Dale

                              Comment


                              • #16
                                Hi Gary,


                                PowerBASIC knows of three variable scopes: "global", "threaded" and "local". Within a scope a variable cannot be redefined. Personally i think this a very wise restriction. There are other languages, which allow for such redefinitions (explicity and implicitly) - and therefore can be a nightmare to debug, if you don´t keep up to a level of coding discipline PowerBASIC automatically imposes on you.

                                With "#DIM ALL" every variable must be defined before it can be referenced, without it you must make use of type specifier. A line defining a variable may be placed everywhere (according to it´s scope outside or inside a procedure) as long as it is before the first reference to the variable.

                                Such a definition line doesn´t create executable code, but it tells the compiler to allocate appropriate space in the programs heap or stack for it. So even when placed inside a loop this is done only once.

                                A variable definition always costs the memory space the variable occupies, regardless, if it actually is used or not. A type definition (UDT) doesn´t cost any space in the executable at all - it tells the compiler about the structure, the order, size and offsets, of it´s members. If it isn´t used - nothing happens. If it is used - the corresponding variable occupies the predefined memory space.


                                JK

                                Comment


                                • #17
                                  Re: my comment is post #3, I used Gary's code from post 1.

                                  I have often put my variables into a separate file and used the include$ feature to place them in the sub/function. As follows:
                                  Code:
                                  SUB example(that AS WHATEVER, etc. AS WHATEVERELSE)
                                    $INCLUDE "example_variables.inc"
                                  
                                  END SUB
                                  since $INCLUDEs can go anywhere in the code as text substitution. This allows quick access to the variables in the IDE with the next tab to the .bas file being the .inc file.
                                  Rod
                                  "To every unsung hero in the universe
                                  To those who roam the skies and those who roam the earth
                                  To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                                  Comment


                                  • #18
                                    His first (upper) code block where he tried to reuse x as a string when it was previously a long in the same sub? I didn't consider that one at all, doomed from git-go, sorry.
                                    Dale

                                    Comment


                                    • #19

                                      Type of variable can't be refined to the best of my knowledge, but using variant would work.
                                      Now getting into COM area where some object experts would know how to do this.
                                      Another way would be to put SUB into a DLL and pass anything with a descriptor so the received string is converted.
                                      Code:
                                      #DIM ALL
                                      
                                      TYPE MyType
                                       s AS STRING * 10
                                      END TYPE
                                      
                                      FUNCTION PBMAIN AS LONG
                                       LOCAL v         AS VARIANT
                                      
                                       LOCAL Strings   AS STRING
                                       LOCAL Singles   AS SINGLE
                                       LOCAL Longs     AS LONG
                                       LOCAL Doubles   AS DOUBLE
                                       LOCAL DWORDS    AS DWORD
                                       LOCAL INTEGERS  AS INTEGER
                                       LOCAL QUADS     AS QUAD
                                       LOCAL Words     AS WORD
                                       LOCAL bytes     AS BYTE
                                       LOCAL curs      AS CUR
                                       LOCAL iPointer  AS INTEGER POINTER
                                      
                                       Test  Singles
                                       Test  Longs
                                       Test  Doubles
                                       Test  Dwords
                                       Test  Integers
                                       Test  Quads
                                       Test  Words
                                       Test  bytes
                                       Test  curs
                                      
                                       LOCAL t AS MyType
                                       t.s = "t.s string wont display"
                                       Test  t AS STRING 'AS STRING required for variant conversion
                                      
                                       Strings = "Strings"
                                       Test Strings
                                       ? "Done"
                                      
                                      END FUNCTION
                                      
                                      SUB Test(v AS VARIANT)
                                       LOCAL typ AS WORD
                                       typ = VARIANTVT(v)
                                       SELECT CASE typ
                                        CASE 8    : ? VARIANT$(v),,"dynamic string"
                                        CASE 16386: ? "integers"
                                        CASE 16387: ? "longs"
                                        CASE 16388: ? "singles"
                                        CASE 16389: ? "doubles"
                                        CASE 16390: ? "curs"
                                        CASE 16401: ? "bytes"
                                        CASE 16402: ? "words"
                                        CASE 16403: ? "dwords"
                                        CASE 16404: ? "quads"
                                        CASE ELSE
                                         ? "type" + STR$(typ)
                                       END SELECT
                                      END SUB
                                      https://www.tesla.com/roadster

                                      Comment

                                      Working...
                                      X