Weird error messages when macros/defines in namespace

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

Weird error messages when macros/defines in namespace

Post by Tourist Trap »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Weird error messages when macros/defines in namespace

Post by fxm »

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: 2958
Joined: Jun 02, 2015 16:24

Re: Weird error messages when macros/defines in namespace

Post by Tourist Trap »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Weird error messages when macros/defines in namespace

Post by fxm »

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: 2958
Joined: Jun 02, 2015 16:24

Re: Weird error messages when macros/defines in namespace

Post by Tourist Trap »

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 :)
Post Reply