Shop OBEX P1 Docs P2 Docs Learn Events
Set constant in object — Parallax Forums

Set constant in object

prolowprolow Posts: 3
edited 2014-06-09 12:48 in Propeller 1
Is there a way to set a constant in a object from a method?
For example:
someObjWithConst.spin
CON
    bla = 0

TopLevelObj.spin
OBJ
    subObj : someObjWithConst

Pub
    subObj.bla := 6

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-05-30 23:54
    Contants, as their name suggests, cannot be changed.

    You can use a constant like this:
    foo := subObj#BLA
    


    You cannot use dot-notation to change variables inside constants, either; you must provide an interface method to make that change.
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-05-31 04:29
    You can refer to constants in sub-objects as Jon showed, but a sub-object can't refer back to constants in the top object. I usually do something like this:

    TopObject.spin
    OBJ
      subObj : "SubObject"
    
    CON
      bla = subObj#bla
    


    SubObject.spin
    CON
      bla = 6
    
  • ksltdksltd Posts: 163
    edited 2014-06-09 11:02
    Declarations in CON sections are simply named numbers, and must be manifest constants. They have no representation at runtime. Therefore, there's nothing to change at runtime.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-09 11:16
    I often wish there were a way of setting constants in a child object based on a constant in the parent.

    Often a child object will have a buffer which needs to be sized to match a constant. It would be nice if these child objects could remain unchanged when these buffers are resized. As it is, I need a separate child object for each size of buffer a program needs.

    Serial objects are a good example of objects which would benefit from this sort of system.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-06-09 11:21
    Duane Degn wrote:
    Often a child object will have a buffer which needs to be sized to match a constant.
    In a case like this, the parent would provide the buffer and tell the child where it is and how big it is.

    -Phil
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-09 11:24
    In a case like this, the parent would provide the buffer and tell the child where it is and how big it is.

    -Phil

    I sometimes go this route but it makes it harder to keep things modular. It would be really nice if there were a "#" type of notation which worked the opposite direction.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-06-09 11:45
    Duane Degn wrote:
    It would be really nice if there were a "#" type of notation which worked the opposite direction.
    The way around this is to have a separate child object that has a dummy start method and, otherwise, just defines constants. Yeah, I know its awkward. Going the other way would require a construct something like this:
    CON
       
      sio#BUF_SIZE = 128
    

    But what if two objects do this to the same child, but with different values? The code and DAT portions of objects are shared amongst all instances. So there would be a conflict. I think Chip was right in not allowing "remote" CONstant definitions.

    -Phil
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-09 12:48
    The way around this is to have a separate child object that has a dummy start method and, otherwise, just defines constants. Yeah, I know its awkward.

    I might start doing this with some of my projects but I was kind of thinking of objects to place in the OBEX where the person using the object wouldn't have to know much about how to set it up to use the object. Though it could be argued (probably correctly) setting constants used by the child in the parent is hardly modular.
    But what if two objects do this to the same child, but with different values? The code and DAT portions of objects are shared amongst all instances. So there would be a conflict.

    In these cases the compiler would need to keep separate copies of the object.
    I think Chip was right in not allowing "remote" CONstant definitions.

    I won't disagree. I'll have to find something else to whine about. Give me a minute and I'll come up with something.
Sign In or Register to comment.