LoginLogin

Structs Library Development Forum

Root / Talk About Programs / [.]

snail_Created:
I want to take this thread to hopefully have better community insight on how I should go about improving the Structs Library. Page comments only get us so far, and I'm even having some questions myself, so I'll be using this thread. Requests and discussions for upcoming features, bug reports, whatever. Next Update: 1.1
  • GENERIC field type
  • TYPEOF function for checking field types
  • CLONE to produce exact copies of structs.
  • Improvements to INSTANCEOF
  • Improvements to errors

I created this thread because I wanted to discuss a main concern of mine: what should happen when getting the TYPEOF a generic field? As the structs evolved I figured I wanted to include generic fields---fields which can contain any datatype (int, float, or string.) The inspiration came from Java; unlike Java, however, the types don't need to be defined at declaration time and it isn't a compiler feature. The nature of these structs allows these fields to maintain any data type at any given time. To provide an example:
@STRUCT_GENERIC
DATA "GENERIC","FIELD"
DATA "END"
This declares a struct type GENERIC with a generic-type field named FIELD. When this struct is instanced, you don't need to provide a type; the standard procedure for declaration holds true. At first, the field simply has no type defined besides "generic.' Let's say you set it to an int, for example:
'here STRC contains a GENERIC struct
SET STRC,"FIELD",1
The field will now contain information stating that it's generic, but also keeps a data type of int inside. If we tried to set the field to a string it would work. The type of the generic field just depends on what data type it's storing. It can store any type at any time. Since we have information that the field is both generic and what data type it holds, what is returned when we do TYPEOF(STRC,"FIELD")? I wanted to ask what you guys would find useful.
  • Return just the type of the data inside the field.
  • Return just "GENERIC".
  • Return some mixture of both.
The issue with the first option is that it doesn't tell the programmer that it's a generic field. It also won't have anything to return if the field hasn't been set yet. The second option tells the programmer that it's generic, but doesn't tell what type the data it holds is. This is the less useful option, because knowing the type of a generic field lets the programmer anticipate its contents. This doesn't provide that. The third option seems like the best of both worlds, but what format will the return be? TYPEOF returns the typename as a string, so if you can come up with a good format that's also easy to use I would be glad to hear it. Another issue is that a generic field contains *nothing* until it's set. If you try to read it there will be nothing there. With the other types it contains the default value. This is less of a problem because I can just make it an error condition, but it's worthy of mentioning.

return "GENERIC" when empty, otherwise return the type of the currently stored value EDIT: could you make a function to add/remove fields? and also a function to get a list of fields in a struct array

return "GENERIC" when empty, otherwise return the type of the currently stored value
Pretty good, actually. I can't imagine generics to be used for much beyond abstraction when building something on top of structs, and if the field is one type once it probably always will be. I'll go with this unless someone comes up with something better lol.
EDIT: could you make a function to add/remove fields? and also a function to get a list of fields in a struct array
This would be very easy but also somewhat out of the scope of this. At that point the struct would just become a dictionary, but I might consider it late on the road if people REALLY want it.