Weird error messages when macros/defines in namespace

General FreeBASIC programming questions.
Tourist Trap
Posts: 2762
Joined: Jun 02, 2015 16:24

Weird error messages when macros/defines in namespace

Postby Tourist Trap » May 20, 2019 15:11

Hello,

I really tried to figure out how we are supposed to interpret this:

Code: Select all

namespace H
    #define V   ?
    #macro U
        V
    #endMacro
    #macro H  'error 120: Expected period ('.') in '#macro H'
        '...
    #endMacro
end namespace
U  'ok
V   'ok
H.U     'error 14: Expected identifier, found '?' in 'H.U'
H.V     'error 14: Expected identifier, found '?' in 'H.V'

It looks like defines ans macros ignored the namespace where were introduced, otherwise they simply fail to work correctly.
Also, when the name of the macro is homonymous to its namespace, it asks for a dot, which is weird.

Do someone know how all this really works?
Thanks.
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Weird error messages when macros/defines in namespace

Postby fxm » May 20, 2019 15:44

Everything is in the documentation:
Defines are scoped; they are only visible in the scope they were defined in. If defined at module level, the define is visible throughout the module. If the identifier is defined inside a compound statement having scope (Sub, For..Next, While..Wend, Do..Loop, Scope..End Scope, etc), the identifier define is only visible within that scope. Namespaces on the other hand do not have any effect on the visibility of a define.
#macro being the multi-line version of #define, => same rules.

So your example above is preprocessed as following:

Code: Select all

namespace H
end namespace
 ?
 ?
H. ?
H. ?
Tourist Trap
Posts: 2762
Joined: Jun 02, 2015 16:24

Re: Weird error messages when macros/defines in namespace

Postby Tourist Trap » May 20, 2019 15:55

fxm wrote:Everything is in the documentation:
...
So your example above is preprocessed as following:

Code: Select all

namespace H
end namespace
 ?
 ?
H. ?
H. ?

Oh thanks. I thought that Namespaces were scoping things.

But for that one the message doesn't make too much sense:

Code: Select all

namespace V
    #define V    ' error 120: Expected period ('.') in '#define V'
end namespace
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Weird error messages when macros/defines in namespace

Postby fxm » May 20, 2019 16:16

As 'V' is the identifier of a declared Namespace, any next 'V' is reserved to declare a new block of the same Namespace ('Namespace V'), otherwise may be followed with '.' as prefix to access a symbol declared in the Namespace 'V'.

- Error: error 120
- Proposal: Expected period ('.')
Tourist Trap
Posts: 2762
Joined: Jun 02, 2015 16:24

Re: Weird error messages when macros/defines in namespace

Postby Tourist Trap » May 20, 2019 16:46

fxm wrote:- Error: error 120
- Proposal: Expected period ('.')

Ok. That makes sense.

It can be confusing until we know it. For example in this case:

Code: Select all

namespace V
    '...
end namespace

'...

#define V    '  error 120: Expected period ('.')
'we may not see that a namespace had been defined previously

Anyway, for me it's clear now. Thank you :)

Return to “General”

Who is online

Users browsing this forum: No registered users and 20 guests