Menu

#895 Different namespace scoping rules apply in type and class declarations

open
nobody
None
compiler
2021-11-07
2019-03-25
TeeEmCee
No

The following code:

TYPE MenuState as integer

TYPE TTT
  DECLARE SUB method()

  menustate as MenuState
  zonemenustate as MenuState
END TYPE

produces the error:
bug.bas(7) error 14: Expected identifier, found 'MenuState' in 'zonemenustate as MenuState'
That's fine. But if you comment out the DECLARE SUB method() line then the TTT type is no longer treated as a class and you get no error, apparently because different rules apply. Strange and unexpected.

Using a git build that's half a year old (just before FB 1.06)

Discussion

  • fxm (freebasic.net)

    The parser is not as smart as the person who codes.
    Some rules, sometimes not completely logical, have been implemented to solve name duplication problems.
    By the way, there are still bugs about it:
    https://sourceforge.net/p/fbc/bugs/645/

    For this particular syntax, you can help parser by meaning when the used name is not local but global:

    TYPE MenuState as integer
    
    TYPE TTT
      DECLARE SUB method()
    
      menustate as MenuState
      zonemenustate as ..MenuState
    END TYPE
    

    The rule:

    To access duplicated symbols defined outside the Type, add two) dots as prefix:
    ..SomeSymbol

     

    Last edit: fxm (freebasic.net) 2021-12-25
  • TeeEmCee

    TeeEmCee - 2019-03-26

    Thanks, that trick hadn't occurred to me.

    I had always assumed that FB has separate namespaces for types and for other identifiers, because you can usually, but not always, reuse the name of a type as a variable name. Is that wrong? Inability of the parser to determine whether an identifier names a type or not in certain contexts could explain the inconsistency I see.

     
  • TeeEmCee

    TeeEmCee - 2021-11-07

    This is a duplicate of [#583]: global type names are shadowed by UDT fields. But in this report I showed that it only happens when the UDT has methods.

     

    Related

    Bugs: #583


Log in to post a comment.

MongoDB Logo MongoDB