Shop OBEX P1 Docs P2 Docs Learn Events
VAR Suggestion — Parallax Forums

VAR Suggestion

Dave HeinDave Hein Posts: 6,347
edited 2011-06-22 05:23 in Propeller 1
VAR variables are useful because it is easy to create multiple instances just by referencing an object multiple times in the OBJ section. However, this causes problems when the same object is referenced by other objects. This creates independent sets of variables when someone really wants to have one set. Newbies and Oldies run into this problem all the time when using objects like FullDuplexSerial.

My suggestion is to have a new object section that forces a single instance to be created unless an object is referenced multiple times within another object. I call the new object symbol SHR for shared. It would look something like this:
OBJ
  ser1 : "FullDuplexSerial" ' Private object -- just like the current way

SHR
  ser2 : "FullDuplexSerial" ' Shared object -- other objects can share this one
Any object that declares FullDuplexSerial in the SHR section will use the same VAR space when it calls an FDS method. I think this would make Spin more useful and easier to use for newbies and oldies.

EDIT: I realize that SHR conflicts with the opcode for shift-right, but it's just an example. Some other symbol could be used instead of SHR.

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2011-06-22 05:02
    especially for serial communication the serial-mirror-object uses variables in the always shared DAT-section.

    I haven't thought about the implications of this solution. Could DAT-variables be a solution to that. Or are DAT-variables harder to handle?

    keep the questions coming
    best regards

    Stefan
  • RS_JimRS_Jim Posts: 1,768
    edited 2011-06-22 05:20
    Dave,
    I think you are on the right track! I struggled for over a year with trying to figure out how to use a global variable with multiple objects. To me, the literature was very unclear as to how to declare a var that could be accessed by multiple objects. Thanks to jonnymac I can now use a VAR shared by multiple OBJ's.
    Jim
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-06-22 05:23
    @Stefan, A DAT section would do about the same thing as a shared VAR section. The main difference is that the programmer would have some control over the variable space that is used by an object. Another possibility would be to pass a pointer to the VAR space that you want an object to use. This would essentially implement structures and structure pointers. The current method for creating VAR spaces in Spin is just too haphazard.

    @Jim, I would like to see a global section also where all objects could directly access global variables. That would be very useful.
Sign In or Register to comment.