Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 46

Thread: TypeOf- ideas

  1. #11
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    I will put into ToDo list for the next version.
    It is quite complex having such a feature all over all possible places where a type can be indicated.
    I will start from DIM/LOCAL/GLOBAL
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  2. #12
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    I think as a Function-result or Parameter it would not be appropriate -
    Function myFun (ByVal s As "someType") As "someType"
    
    ' neither this:
    Type t_myType
       X As "someOtherType"
    End Type
    
    would be ... nonsense?

    But like this:

    Function CreateSomething(Byval what as String) As Dword
    
      Local localData as what
      
      Function = Heap_Alloc(SizeOf(localData))
    
    End Function
    
    ' ... in some other function:
    String current_Type = "t_myType"
    
    Local virtualDummy As current_Type At CreateSomething(current_Type)
    
    would serve. Maybe use another keyword, such as SuchAs
    Last edited by ReneMiner; 19-10-2013 at 01:10.
    I think there are missing some Forum-sections as beta-testing and support

  3. #13
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    ok, we have the "like" now, but still limited to simple variables - somehow you did it to do a trick with the string-content in pre-parsing


    I just collect some thoughts for the udts where the "like" could be very powerful and useful, especially to "privatize" data.

    The usage for udts could be limited to local virtual overlays only
    - because that's the reason for the idea:
    to dim a LOCAL virtual overlay inside some multiple useable function at some already existing piece of memory,
    the memory mostly unstructured as heap or string, stored as array or single element of some udt
    -that udt and the allocated memory have to exist the moment when the function gets called already...

    It would serve if a string containing the udts-name or pattern has to be passed mandatory as a parameter somehow to the function where the virtual overlay
    Local foo Like theUdtIPassedInThatString At memoryptr
    
    will be done, but to be dynamic this would need the reverse way to retrieve the types-name from some variable or its pointer into a string also i fear.

    So would be fine too if some core-function would just return some in every script-execution varying long/byte/whatever/ "temporary constant for duration of this execution" -numeral that can be used to dimension the overlay later - just to save information about the desired udt somehow.

    Long myType = Type_GetHandle(myUDT) 
    
    'where myUDT could be some udt-name or some variable of that udt 
    ' -  both have pros & contras  
    ' I'd prefer to retrieve it from a variables name - or even from its pointer
    ...
    Local foo Like myType At memoryptr
    ' foo.Init()
    
    since all types of all units are known when the script starts running...

    I know, it's complicated. Else I would have found a basic solution already
    Last edited by ReneMiner; 16-03-2014 at 09:14.
    I think there are missing some Forum-sections as beta-testing and support

  4. #14
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    I was able to get something, not sure it is 100% what you asked but maybe on the right track.
    Download again http://www.thinbasic.biz/projects/th...c_1.9.12.0.zip

    Example:
    Type lType
      i As Integer
      l As Long
      
      Mult As Function
    End Type
    
    
    Function lType.Mult() As Long
      Function = Me.i * Me.l
    End Function
    
    
    Dim MyType As lType
    
    
    Function FunctionExample(ByVal p As Long) As Long
      Dim x1 As Byte
      Dim x2 Like "byte"      '---Like [any string expression representing a basic type]  
      X1 = 1
      x2 = 2
    
    
      Dim y1 As p             '--- Define [Y1] with the same type of [p] variable
      y1 = 10 * p
      
      Dim z1 As MyType        '--- Define [z1] with the same type of [MyType] variable
      z1.i = 10
      z1.l = 100
      MsgBox 0, z1.Mult
      
    End Function
    
    
    FunctionExample(1000)
    
    
    MsgBox 0, "Finished"
    
    Main rules so far:

    • AS can be followed by a variable name. ThinBASIC will use that variable type to declare a the new variable.
    • LIKE can be follower by a string expression representing the type. Only basic types can be used so far, not UDT at the moment.
    • You ca use those new options only when declaring a new variable. It will not work inside UDT declaration and Function parameters
    Last edited by ErosOlmi; 17-03-2014 at 07:41.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #15
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    almost there

    If AS can dim some variable from the name of another variable, would it be possible for LIKE to dim some variable from the name of another variable stored in a string? If yes, why should not be possible to do have LIKE to use the way "Dim X Like "lType"

    And could it be possible to have a special "Identify ME"-function then, which will always be to find inside some type-function - mostly X.Create() -
    but not necessarily the same type as the functions parenting type because could be some extended child of that type too.

    such as:
    String udtname = Identify Me

    so in the end we could simply store the "child-Type"-information in the base-type and use the stored information to place a fitting overlay onto some chunk of data, then this chunk of data all of a sudden is able to perform own common "methods", i.e. Type-functions on data which is not stored in a global UDT-variable, so data is not accessable globally but needs to use the lowest base-types rules ("class object") to get access to that "family"...

    wait a second... ...what are we doing here?
    I think there are missing some Forum-sections as beta-testing and support

  6. #16
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Download again http://www.thinbasic.biz/projects/th...c_1.9.12.0.zip

    Example:
    Type lType
      i As Integer
      l As Long
      
      Mult As Function
    End Type
    
    
    Function lType.Mult() As Long
      Function = Me.i * Me.l
    End Function
    
    
    Dim MyType As lType
    
    
    Function FunctionExample(ByVal p As Long) As Long
      Dim x1 As Byte
      Dim x2 Like "byte"      '---Like [any string expression representing a basic type]  
      X1 = 1
      x2 = 2
    
    
      Dim y1 As p             '--- Define [Y1] with the same type of [p] variable
      y1 = 10 * p
      
      Dim z1 As MyType        '--- Define [z1] with the same type of [MyType] variable
      z1.i = 10
      z1.l = 100
      MsgBox 0, z1.Mult
    
    
      Dim kk As Byte
      
      Dim z2 Like "MyType"        '--- Define [z2] with the same type of [MyType] variable using Like followed by a string expression
      z2.i = 100
      z2.l = 1000
      MsgBox 0, z2.Mult
    
    
      Dim z3 Like "lType"        '--- Define [z3] with the same type of [tType] TYPE using Like followed by a string expression
      z3.i = 1000
      z3.l = 10000
      MsgBox 0, z3.Mult
      
    End Function
    
    
    FunctionExample(1000)
    
    
    MsgBox 0, "Finished"
    
    Main rules so far:

    • AS can be followed by a variable name. ThinBASIC will use that variable type to declare a the new variable.
    • LIKE can be follower by a string expression representing the type. Basic types, UDT variable and UDT types are supported.
    • You can use those new options only when declaring a new variable. It will not work inside UDT declaration and Function parameters


    Now the problem will be to describe all those new features in Help File
    Last edited by ErosOlmi; 17-03-2014 at 11:51.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  7. #17
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170

    Now it "only" needs a way to retrieve & save the correct string of type from any variable and it's perfect!

    String Datatype = TypeOf(Me)
    ...
    Local data Like DataType at myPtr
    
    I think there are missing some Forum-sections as beta-testing and support

  8. #18
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Rene,

    why just not using something like
    Local data As Me At VarPtr(Me)
    
    It is already working but IT HAS TO BE EXECUTED from inside the Function Type to which Me refers.
    Let me know.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #19
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    I tested a little... and came to the result it does not work as expected so I did not finish this to more functionality yet - has a couple of useless functions now, but shows what I was up to. Because no retrieving of type possible I just pass it as parameter on Create

    The types get recognized and used correctly - but just once per script execution. Calling twice the same sub with a different type to use will always dim the type again that was dim'ed the very first time... The example is a little longer, for try out/ see what I mean just exchange the order of the Test-call at end of script
    #MINVERSION 1.9.12.0.
     
    Uses "console"
     
    Type t_virtual_object
      pData  As DWord
      sType As String   
      lSize As Long
       
      Create  As Function   
      Destroy As Function   
      
    End Type
     
    Function t_virtual_object.Create(ByVal sType As String, Optional ByVal lNum As Long) As DWord
     
      Local data Like sType  
      
      If lNum < 1 Then lNum = 1     ' = Ubound/Index
      
      Me.pData = HEAP_Alloc(SizeOf(data) * lNum) 
      Me.sType = sType
      Me.lSize = SizeOf(data)   ' store size of one element, not to get too complicated
      
      Return Me.pData
       
    End Function
     
    Function t_virtual_object.Destroy() As DWord
       
      If HEAP_Size(Me.pData) Then HEAP_Free(Me.pData) 
      Me.pData  = 0   
      Me.sType = ""
      
      Return 0
       
    End Function  
    
    ' - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    Type t_vec3d     
      ' some simple vector with a few functions for example
      X As Double
      Y As Double
      Z As Double
      
      GetProperties As Function
      
      GetX As Function
      GetY As Function
      GetZ As Function
      
      SetX As Function
      SetY As Function
      SetZ As Function
      SetXYZ As Function    
    End Type
    
    Function t_Vec3d.GetProperties() As String
      Function = "t_Vec3d has X,Y and Z"
    End Function
    Function t_vec3d.GetX() As Double
      Return Me.X    
    End Function 
    Function t_vec3d.GetY() As Double
      Return Me.Y    
    End Function 
    Function t_vec3d.GetZ() As Double
      Return Me.Z    
    End Function
     
    Function t_vec3d.SetX(ByVal X As Double) As Double
      Me.X = X
      Return X    
    End Function 
    Function t_vec3d.SetY(ByVal Y As Double) As Double
      Me.Y = Y
      Return Y    
    End Function 
    Function t_vec3d.SetZ(ByVal Z As Double) As Double
      Me.Z = Z
      Return Z    
    End Function 
    Function t_vec3d.SetXYZ(ByVal X As Double, ByVal Y As Double, ByVal Z As Double) As Double
      Me.X = X
      Me.Y = Y
      Me.Z = Z
    End Function 
    ' - - - - - - - - - - - - - - - - - - - - - - - - - 
    
    Type t_RGB      
      ' and some color-type with functions also
      
      R As Byte
      G As Byte
      B As Byte
      
      GetProperties As Function
      
      GetR     As Function
      GetG     As Function
      GetB     As Function
      GetColor As Function     
      
      SetR     As Function
      SetG     As Function
      SetB     As Function
      SetRGB   As Function
      SetColor As Function
      
    End Type
    Function t_RGB.GetProperties() As String
      Function = "t_RGB has R,G and B"
    End Function
      
    Function t_RGB.GetR() As Byte
      Return Me.R
    End Function
      
    Function t_RGB.GetG() As Byte
      Return Me.G
    End Function
    
    Function t_RGB.GetB() As Byte
      Return Me.B
    End Function
    
    Function t_RGB.GetColor() As Long
      Local lColor As Long 
      Memory_Copy( VarPtr(Me), VarPtr(lColor), 3)
      Function = lColor
    End Function
    
    Function t_RGB.SetR(ByVal R As Byte) As Byte
      Me.R = R
      Return R
    End Function
      
    Function t_RGB.SetG(ByVal G As Byte) As Byte
      Me.G = G
      Return G
    End Function
    
    Function t_RGB.SetB(ByVal B As Byte) As Byte
      Me.B = B
      Return B
    End Function
    
    Function t_RGB.SetColor(ByVal lColor As Long) As Long
       Memory_Copy(Varptr(lColor),Varptr(Me),3)   
      Function = lColor
    End Function
    ' -------------------------------------------------------------
    ' three different types now
    
    ' have some global "objects":
    
    Dim Colors As t_virtual_object
    Dim Vectors As t_virtual_object
    
    Vectors.Create("t_vec3d", 12) ' space for an array of 12 vecs
    Colors.Create("t_RGB", 34)    ' and some space for 34 colors
    
    ' now do the rest inside some subs/functions to keep the stuff "private" 
    Sub Test(what As t_virtual_object)
      
      If HEAP_Size(what.pData) < 1 Then PrintL "Error- no data!": Exit Sub
      
      Local data(HEAP_Size(what.pData)/what.lSize) Like what.sType At what.pData
      
      PrintL data(1).GetProperties
    
    End Sub
    
    Test Colors
    Test Vectors
      
    PrintL "----------------------------"
    PrintL "All done, press a key to end"
    WaitKey
    
    Last edited by ReneMiner; 17-03-2014 at 13:58.
    I think there are missing some Forum-sections as beta-testing and support

  10. #20
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    oops- I just saw I added a SetRGB in the Type-definition but there's no function - and no errror - even it's not an extended Type
    Last edited by ReneMiner; 17-03-2014 at 14:47.
    I think there are missing some Forum-sections as beta-testing and support

Page 2 of 5 FirstFirst 1234 ... LastLast

Similar Threads

  1. OOP ideas
    By ErosOlmi in forum Suggestions/Ideas discussions
    Replies: 13
    Last Post: 26-08-2013, 20:26
  2. More Ideas
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 3
    Last Post: 27-10-2012, 14:47

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
  •