Shop OBEX P1 Docs P2 Docs Learn Events
Spin 2 - cannot use a local variable/function parameter name in dat section - bug or feature? — Parallax Forums

Spin 2 - cannot use a local variable/function parameter name in dat section - bug or feature?

pik33pik33 Posts: 2,366
edited 2021-02-13 12:33 in Propeller 2

I am trying to convert a video driver from p1 to p2 using a Propeller Tool 2.4.1.

There is a method: pub setfontcolor(line,pos,r,g,b) | temp, place, color

It returns an error: "Expected a unique parameter name"

It seems it is because of "line" being a label in in the dat section in the same file: a pasm code to run in another cog via coginit.

After I renamed a PASM label to 'line2' it compiled OK

This means I cannot use a spin method's local - local! variable names in dat section... I have to remember them all - why? They are local, these names should be not visible outside a method.

Or is there any trick I don't know as a P2 beginner?

Comments

  • cgraceycgracey Posts: 14,152

    It's not that the local variables are visible outside the method, but that DAT variables are visible within the method, right? DAT variables need to be visible everywhere in the file, unlike local method variables. Maybe I'm not understanding, or you just disagree with how it works.

  • pik33pik33 Posts: 2,366
    edited 2021-02-13 15:18

    So I simply have to remember about this and avoid using the same names in spin method and dat sections.

    The first time I encountered this error, there was a confusion: what the compiler wants? In "bigger" languages local declared variables simply hide the global ones if there is a conflict and this was the source of the confusion.

  • Cluso99Cluso99 Posts: 18,069

    Presuming I understand this correctly...

    Within an object

    • a spin method can have local variables eg line is local in this pub xyz(abc) : retn | line
    • spin within the object has global variables (within the object) declared within a VAR section
    • AND spin has global-global variables declared (within the whole program) declared within a DAT section
    • This DAT section is common and gets compiled in once, even though the object may be declared multiple times, and the VAR is compiled in once for each time the object is declared, and BTW the spin code (pub and pri routines) is only compiled in once because it is re-entrant code and may be called concurrently.

    Hope I have this correct ;)

  • pik33pik33 Posts: 2,366

    And then the names in DAT sections have absolute priority and cannot be reused anywhere else, or the compiler will generate an error. So if there is "line" label in DAT, you cannot have pub xyz() | line. One of these names has to be changed ot the program will not compile

  • pik33pik33 Posts: 2,366

    As I can remember, there was a topic somewhere with P2 tips and traps, maybe we need to make this get pinned on top of the P2 forum to make first steps with P2 easier.

    Another trap: don't mix defined longs with res. It will compile, but a long defined after res will become another undefined res.

Sign In or Register to comment.