Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: For discussion: simplified UDT / array of UDT initialization.

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,089
    Blog Entries
    5
    Rep Power
    725

    For discussion: simplified UDT / array of UDT initialization.

    Looking at Zak's code, one idea from the past returned to my head - simple UDT initialization.

    Lets take a classic case of UDT for vector:
    Type Vector3D
      x  As Single
      y  As Single
      z  As Single
    End Type
    
    Dim myVector As Vector3D
    
    Now, we can initialize the variable in two ways (leaving aside hacks ):
    Way #1
    myVector.x = 1
    myVector.y = 2
    myVector.z = 3
    
    Way #2
    With myVector
      .x = 1
      .y = 2
      .z = 3
    End With
    
    Both are easy to understand, but also quite lenghty.

    In past years I suggested to make possible something like:
    myVector = (1, 2, 3)
    
    Eros had good note, that this way of initialization is potentially dangerous, as it heavily depends on order of definition of UDT members. If you, for example during code cleanup, rearrange the order of UDT members, it immediately makes code damaged.

    This time, I have slightly modded proposal, what about this syntax:
    myVector.x,y,z = (1, 2, 3)
    
    This way:
    • You get shorter form, still clear
    • Possibility of member mismatch is low, as you explicitly specify which members you want to initialize (in this case x, y, z in order as written)
    • It would allow arbitrary stunts, such as filling only the members listed -> myVector.x,y = (1, 2) ' -- Leaves z untouched

    The brackets might seem redundant, but would be useful for initializing arrays of UDTs:
    Dim myVectors(3) As Vector3D
    
    myVectors().x,y,z = (1, 2, 3), (4, 5, 6), (7, 8, 9)
    
    Only problem I see now is performance - for sure this won't be fast, as parser has to check type for each member in the sequence, to get the value. But from my side this is something I could live with, as the comfort it provides and the selfdocumenting factor seem to be the thing I would use this for.

    I would be curious about your opinion on this proposal. I did not posted this in the Support as new Suggestion issue, as I wanted to discuss it first with others.
    Do you see any weak points in this approach (like my original old proposal indeed had?). Would you like to see something like this implemented?


    Thanks,
    Petr
    Last edited by Petr Schreiber; 20-08-2011 at 20: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

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    54
    Posts
    8,636
    Blog Entries
    2
    Rep Power
    10
    Petr,

    because I noted zak's code too ... I was 100% sure you would have revamped that idea
    I like very much your new concept. The problem is that it is not so easy to implement.

    But let's talk about it a little more, in the meantime I will think about how to implement because in zak's code the main UDT contains an array so the solution must work on whatever nested level of arrays: array of an UDT that contains another UDT that contains an array, .... ans so on.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  3. #3
    Welcome back Eros.

    MultiAssign proves to be very useful for filling simple structures, like vectors and arrays of vectors, and also arrays of primitives.

    in Oxygen I use '=>' or '<=' notation. This is much easier for the parser than plain '='.

    Arrays can be filled from any index point like this:

    a[30]<=2,4,6,8,10 ...

    Outer brackets must be used when assigning within a dim statement

    dim as a[100]<=(2,4,6,8,10)

    With vector arrays, I don't support inner brackets, tabular layout is better.

    type LabelledPoint single x,y,z, string s
    dim as LabelledPoint v[100]
    v<=
    0,1,2,"point A",
    0,3,4,"point B",
    1,1,2,"point C",
    1,3,4,"point D"

    Complications like arrays inside structures are not directly supported. The programmer must use overlays mapped to these internal arrays instead. Unions are another hazard. But in the cause of clean programming I recommend these difficult cases be cheerfully ignored

    Charles
    Last edited by Charles Pegge; 21-08-2011 at 08:18.

  4. #4
    Quote Originally Posted by Petr
    myVector.x = 1
    myVector.y = 2
    myVector.z = 3
    I find using associative arrays in ScriptBasic easier to use.

    This is simulating a structure using labels as indexes to the array.

    myVector{"x"} = 1
    myVector{"y"} = 2
    myVector{"z"} = 3
    

    In this case x,y,z are variables (numeric or string) that define the index of the array.

    x = 0
    y = 1000000
    z = "aBc"
    
    myVector{x} = 1
    myVector{y} = 2
    myVector{z} = 3
    
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    54
    Posts
    8,636
    Blog Entries
    2
    Rep Power
    10
    ---Charles

    your method is clear and elegant but, as Petr mentioned above, it is risky because if the defined TYPE change the order of the elements programmer has to change all assignments accordingly otherwise subtle errors can be just around the corner.


    ---John

    The problem is that associative arrays are not real memory structures that can be passed to API functions when requested but they are more like hash tables with key/data pairs.
    And in that structures (associative arrays/hash tables) memory is not allocated continuously but randomly.


    Thanks to all for this discussion.
    Going on thinking ...
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  6. #6
    Quote Originally Posted by Eros
    The problem is that associative arrays are not real memory structures that can be passed to API functions when requested but they are more like hash tables with key/data pairs.
    And in that structures (associative arrays/hash tables) memory is not allocated continuously by randomly.
    I feel that a Basic language should interact with the user at the most simplest and highest level. The complexity of API interfacing should be done within an extension module. I can pass a C based extension module a ScriptBasic array variable and build whatever structure needed to interface with another library within the extension module. That is why it is important that the core Basic have a API of it's own to make interfaces seamless.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,089
    Blog Entries
    5
    Rep Power
    725
    Hi John,

    having UDTs directly manipulable in ThinBASIC, there is no "API complexity" involved. I declare UDT, fill it, pass it to DLL function without any conversion or layer between. Job done
    The only thing I would like to see improved (in specific cases), is the filling from code described in the first post.

    What you describe is interesting, but it would not be suitable for my cases of use, that is interfacing to OpenCL, CUDA and OpenGL from thinBasic. I need to stay low level, converting huge hash tables to structures passable to DLL would eat too much time for me in these situations.

    I know the approach you are talking about from Python, it is an option (especially in typeless languages), but I am not interested to see something like this in ThinBASIC at the moment.


    Petr
    Last edited by Petr Schreiber; 21-08-2011 at 11:50.
    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
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    54
    Posts
    8,636
    Blog Entries
    2
    Rep Power
    10
    Statically typed languages seems revamping over dynamically typed ones.


    Source: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  9. #9
    Quote Originally Posted by Petr
    What you describe is interesting, but it would not be suitable for my cases of use, that is interfacing to OpenCL, CUDA and OpenGL from thinBasic. I need to stay low level, converting huge hash tables to structures passable to DLL would eat too much time for me in these situations.
    In a previous code challenge, the ScriptBasic SPLITA directive split all the words in the Bible (1.4 million element array) in less than a second. I'm sure passing a SB array and having a C based SB API function convert it to a structure shouldn't be that costly.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  10. #10
    Types are a major issue in programming. To get good performance you need to end up with types that the processor can handle directly. I found It is in fact possible to have typed and type-less directly available in the same language at least for primitives. To support this, the compiler/interpreter must provide automatic conversion between all the basic types used in an expression and then convert the result back to the correct form for variable assignment. This is hard work for the interpreter but nice for the user, especially for casual programming, where you want a quick result from a one-off program.

    Coming back to Multi-Assign. I agree it is not without its hazards, if the order of elements is changed. It is most suitable for well known structures like vectors, complex numbers, matrices and dictionaries. I think it is also safe to use within class constructors, where they are usually nearby on the page to the class structure definition.

    Charles

Page 1 of 2 12 LastLast

Similar Threads

  1. Classes Simplified
    By Charles Pegge in forum O2h Compiler
    Replies: 12
    Last Post: 07-05-2009, 07:34
  2. Problems / Core / ASCIIZ initialization
    By Petr Schreiber in forum Preview testing
    Replies: 1
    Last Post: 14-09-2008, 18:44

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
  •