Results 1 to 8 of 8

Thread: Static UDT-idea & more

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174

    Static UDT-idea & more

    Because UDTs can have static members too I came to an idea that reminds a little to vb. There constant enumerations work like the following example:

    Enum Direction [As Long]
      Left   = 1 
      Right
      Up
      Down
    End Enum
    
    imagine this translated to tB:
    Type Direction
      Static Left As Long   = 1 ' i know currently not possible to assign value here
      Static Right As Long  ' will be 2 now...
      Static Up As Long    ' these anyway will remain constantly same value
      Static Down as Long
    End Type
    
    thereafter in vb it's not necessary to "Dim anyVar As Direction" but one can use instantly
    doSomething( Direction.Up )  ' assume doSomething is a Sub here...
    
    now what's better about this then the current way as below?

    Begin Const
      %Direction_Left = 1
      %Direction_Right
      %Direction_Up
      %Direction_Down
    End Const
    
    imagine to have some autocomplete-function in some (still to develop) smart thinBasic-IDE that will bring up a list of possible subitems of types, classes and enumerations when pushing the dot-key (.) Some IDE that not just lists Udts, Functions, Subs and Callbacks in codebrowser but also only shows one function or declarations-section only per code-window, one, that is "project-oriented" - means, it loads all used #include-files to editor and also has a variables-list-tab next to codebrowser that shows global and local to current functions variables & their types. "Declarations-section" is just like code that is not contained within any function.

    One that inserts "Then" automatic when user types "If ". Just push like Shift+TAB-Key to jump behind the "Then" or Shift+Return-Key to go into the next line without adding code behind "Then". The IDE will be smart enough to add some "EndIf" one line below if nothing follows "Then" . And of course - if one types "ElseIf " - automatic "Then" added too. Same behaviour of course when codeline in declarations section starts with Function/Sub/Callback Function: It'll automatic switch from declarations-section to the new Functions/Subs code and of course it has "End Function/End Sub" added automatically then.
    Another feature: Pushing double-quote will automatic make double-quotes twice- one at current caret-position and one thereafter- press Shift+Tab here also to get out of the quoted part in same line, Shift+Return will jump out of the quoted part into the next line while just return will move the end-quote into the next line.
    Of course all "Begin" will automatic add the "End" - no matter if Select Case, While, Do or whatever. And as mentioned - typing a dot outside quotes will bring up a list of possible members to choose from.

    Oh yes, I forgot... the variables-list (tab next to codebrowser) inserts on click on a listed variable even this variable at current caret position. Variables list looks somewhat like this:

    ' this is source code
    ' Declarations-Section
    #Include "thinBasic_gl.Inc"
    
    Type t_vec3d
      X As Double
      Y As Double
      Z As Double
      Static num As Long  ' holds "number_of_myVecs"
      Test as Function
    End Type
    
    Dim keepRunning As Boolean = True
    Dim myVec() As t_vec3d
    
    Dim foo as String = "Byte"
    '...
    
    ' this is current edited function displayed in code-editor
    Function Vec3d_GetID( Byval X as Double, Byval Y as Double, Byval Z as Double) As Long
      
      Local lID As Long
      Local toFind As t_Vec3d
      
      ' just for Fun:
      Dim noSense Like foo
    
      toFind.X = X
      toFind.Y = Y
      toFind.Z = Z
      
      
      If toFind.num > 0 Then    
        lID = Array Scan myVec For toFind.num, Byte(1, SizeOf(t_vec3d)), = toFind     
      EndIf
    
      If lID Then 
      ' found this vec3d:
        Return lID
      Endif
        
      ' need a new one...
    
      If toFind.num = 0 Or toFind.num >= UBound(myVec) Then
        ' allocate more space if reach UBound
        ReDim Preserve myVec(toFind.num + 1000)
      EndIf
      toFind.num += 1
      myVec(toFind.num) = toFind
    
      Function = toFind.num
    
    End Function  
    
    
    ' #####################################
    ' this is variables list, another Tab next to codebrowser, "local" depends on current edited function:
    
    Global
    
      keepRunning, Boolean
      myVec,  t_vec3d,  Array
      foo, String
    
    Local 
    
      X, Double, parameter ByVal
      Y, Double, parameter ByVal
      Z, Double, parameter ByVal
      
      lID, Long
      toFind, t_vec3d
      noSense, Variable
    
    ' clicking onto a variable here will insert it to code-window at current caret position
    
    ' #####################################
    ' and this is codebrowser-tree, all expanded and contains lots of information
     
    -Project "myProject"   ' equals a folders name on HD
      -Includes
         "MAIN.tBasic"    
         "thinBasic_gl.Inc"
      -UDTs
        -t_Vec3d    
           X As Double
           Y As Double
           Z As Double
           Static num As Long
           Test As Function
      -Declarations
         -"thinBasic_gl.Inc"
           Function glAreTexturesResident 
           Function gl... there's a lot of...
           Sub glAccum
           Sub gl... also a whole lot of subs declared within thinBasic_gl.Inc...
      -Functions
         -"MAIN.tBasic"
           TBMain    '(this always listed as first)
           Vec3d_GetID 
      +Subs
      +Callbacks
      +Notes
    
    Difference is, all Functions, UDTs etc of all to project #Includeds are listed in this tree, oriented to the whole project.

    OK, that went a little out to some more than just constant type-lists abusing static udt-members as global const enumerations, but I think it contains ideas...
    I'm still not sure which software (i.e. "Control") to use to write this editor, currently I prototype in TBGL(2d) because RTF is cumbersome as hell and common textbox does not allow colored code. (now since optional parameters in Type-functions work, my new nodes also work finally and also my TBGL-Treeview-Gadget - but coding itself in a TB-written editor is a little lagging, especially when it comes to create the list of possible expressions for autocomplete and on re-scanning the current function for "AS" and "LIKE" on every Return-Key hit and always re-scan if a section of code was marked before...(think of CTRL+X etc.) or if CTRL+V was pushed.

    I hope one of you can see the need of some smart IDE too, one that offers a lot of overview, minimizes the amount of bugs where possible (automatic parenthesis-count in codelines, adding End to any started etc.) and increases coding speed also. Still a lot to do... maybe someone with some experience in creating coding environments will overtake and be faster...
    Last edited by ReneMiner; 09-11-2013 at 13:07.
    I think there are missing some Forum-sections as beta-testing and support

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Hi Rene,

    I agree with your argument 100%, but I would prefer to have ENUM instead of having it in TYPE. Even latest PowerBASIC has implemented it this way, and I consider it good.
    No need to come with new syntax, let's follow the VB/PB style - it is clear.

    What I would add is to make possible to declare variable as ENUM - it will allow great run time checking opportunities. Imagine this:
    Enum Direction
      Left   = 1 
      Right
      Up
      Down
    End Enum
    
    Dim myVar As Direction
    
    myVar = Direction.Left ' -- Valid
    myVar = 1 ' -- Valid, it matches the enum range
    myVar = 5 ' -- Triggers run time error / exception, because you assigned out of range
    

    Petr
    Last edited by Petr Schreiber; 10-11-2013 at 12:34.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Incidentally, thinBasic supports static members within Types. Good for keeping global-space clean. Assign any value to the static member of a variable, then all other variables of the same type will see it.

    type StatType
           a as long
    static b as long
    end type
    
    
    
    
    sub ff()
    dim as StatType f
    f.b=4
    end sub
    
    
    sub gg()
    dim as StatType g
    msgbox 0,g.b 'result 4
    end sub
    
    
    ff()
    gg() '4
    

  4. #4
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    Quote Originally Posted by Petr Schreiber View Post
    Hi Rene,

    I agree with your argument 100%, but I would prefer to have ENUM instead of having it in TYPE. Even latest PowerBASIC has implemented it this way, and I consider it good.
    No need to come with new syntax, let's follow the VB/PB style - it is clear.

    What I would add is to make possible to declare variable as ENUM - it will allow great run time checking opportunities. Imagine this:
    Enum Direction
      Left   = 1 
      Right
      Up
      Down
    End Enum
    
    Dim myVar As Direction
    
    myVar = Direction.Left ' -- Valid
    myVar = 1 ' -- Valid, it matches the enum range
    myVar = 5 ' -- Triggers run time error / exception, because you assigned out of range
    

    Petr
    That's different. There's no need to "dim myVar as Direction" and it would not make much sense I think, but the Direction-enumeration can be used instantly as such. Of course it should be constant and it's just to "group members of the same family" into some readable expression. Static UDT-abusement is not the way it should work finally, but a step on the way to get there since it's organized similar.
    In the example above
    Dim myVar as Direction
    
    can be translated in mind only through
    Dim myVar as Long
    
    because there's no myVar.Left, myVar.Up etc but you can use
    Dim myVar as Long Value Direction.Down
    
    and as soon as one has typed the dot and tokenizer recognizes the thing before the dot as some unquoted string it compares all global in current project dimensioned and all local in current function dimensioned udt-variables if there name matches (exception "Me" - there just check of which udt the function is, contained in the current functions name) and pops up some list of the members to choose from, either mousewheel+click or arrowkeys + shift & right arrow to pick, shift & backspace removes dot and token. Any other key closes the list and continues accepting usual type-in.
    Now would be nice to use that feature on enumerations too.
    I tried like tokens that start with "%" and to popup the list when "_" (underscore) is typed, but this is very much lagging and annoying, especially when typing stuff like "%WS_Ex_..." because it pops up the list too early ("%WS_...") and shows also members of the "wrong family" to choose from then... I want it to be smarter somehow.
    Last edited by ReneMiner; 11-11-2013 at 09:54.
    I think there are missing some Forum-sections as beta-testing and support

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Hi Rene,

    I think these two concepts do not collide - both would be valid:
    Enum Direction
      Left   = 1 
      Right
      Up
      Down
    End Enum
    
    Dim myVar1 As Direction = Direction.Down
    Dim myVar2 As Long = Direction.Down
    
    The first would internally initialize myVar1 as Long but with range checking enabled (in this case values in range 1..4 would be valid), the second would be check free and work as you specified.


    Petr
    Last edited by Petr Schreiber; 11-11-2013 at 10:17.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6
    OxygenBasic originally had dot enumerations, like types, but I pulled this feature, when I saw that C does not use them, and there were few situations where they were an improvement over simple enum/equates.

    This is what it looked like (Generated a lot of complexity inside the compiler!)

    enum fruit
      orange=1
      lemon
      apple
      pear
    end enum
    
    
    
    
    print fruit.lemon '2
    
    
    dim as fruit f=apple
    
    
    print f  '3
    
    
    function ShowFruit(fruit fr)
     print fr '3
     select case fr
        case fruit.orange: print "orange"
       case fruit.lemon: print "lemon"
       ...
     end select
    end function
    
    
    ShowFruit f
    
    Last edited by Charles Pegge; 11-11-2013 at 13:24.

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Hi Charles,

    yes, this is very close to what I would like to see.
    I think it is very practical - especially when the bound checking would be implemented.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  8. #8
    Hi Petr, Rene,

    It looks good in theory, and it's logical, but I think that in practice, it has limited uses because it is a static equate and easily substituted by using name prefixes instead of type-dots eg: Fruit_lemon


    Keeping these symbols as simple integer equate avoids a lot a hassle with type matching and other language considerations.

    Actually, Oxygen will support whole dotted names, so thay can be used if so desired, but it's an experimental thing and I am not sure I would recommend it, since dots normally denote compound variable members.

    Though it might give you the desired semantics, and is quite easy to implement.

    enum fruit
    fruit.orange=1
    fruit.lemon
    fruit.apple
    fruit.pear
    end enum


    int f=fruit.apple '3


    print f
    Last edited by Charles Pegge; 11-11-2013 at 18:59.

Similar Threads

  1. STATIC elements in UDT
    By ErosOlmi in forum thinBasic vaporware
    Replies: 11
    Last Post: 12-05-2009, 07:29
  2. Do you know: STATIC variables
    By ErosOlmi in forum Do you know ...
    Replies: 5
    Last Post: 05-08-2008, 11:03
  3. STATIC variables
    By ErosOlmi in forum Suggestions/Ideas discussions
    Replies: 16
    Last Post: 26-03-2008, 22:00

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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