Page 1 of 1
Defined() and #PB_Macro
Posted: Sat Apr 22, 2006 7:25 pm
by Flype
Result = Defined(Nom, Type)
'Type' can be one these values :
#PB_Constant
#PB_Variable
#PB_Array
#PB_LinkedList
#PB_Structure
#PB_Interface
#PB_Macro ???????????
It could be useful to add #PB_Macro, so that i could write things like this :
Code: Select all
CompilerIf Defined(ENLINK, #PB_Structure) = #False
Structure ENLINK
nmhdr.NMHDR
msg.l
wParam.l
lParam.l
chrg.CHARRANGE
EndStructure
CompilerEndIf
CompilerIf Defined(MAKELONG, #PB_Macro) = #False
Macro MAKELONG(loWord, hiWord)
( hiWord << 16 | loWord )
EndMacro
CompilerEndIf
Posted: Tue Jan 09, 2007 1:36 pm
by Flype
Actually, i just discovered that a Macro seems considered as a Constant by the Defined() compiler command.
I can say this because i tried the following code and it works as expected :
Code: Select all
#PB_Macro = #PB_Constant
CompilerIf Defined(InStr, #PB_Macro)
Debug "THIS MACRO IS ALREADY DEFINED !"
CompilerElse
Debug "THIS MACRO IS NOT DEFINED !"
Macro InStr(string, find, start = 1)
FindString(string, find, start)
EndMacro
CompilerEndIf
Debug InStr("PureBasic", "Basic")
That's strange because in resident files Macros are differents than Constants.
And the biggest problem is :
What's happened if a Macro has the same name as a Constant ?
Code: Select all
#InStr = "Dummy" ; ahahah - now it can't works as expected !
#PB_Macro = #PB_Constant
CompilerIf Defined(InStr, #PB_Macro)
Debug "THIS MACRO IS ALREADY DEFINED !"
CompilerElse
Debug "THIS MACRO IS NOT DEFINED !"
Macro InStr(string, find, start = 1)
FindString(string, find, start)
EndMacro
CompilerEndIf
Debug InStr("PureBasic", "Basic")
So what should we understand about this :roll:

Posted: Fri Mar 30, 2007 2:08 pm
by Flype
** bump ** (sorry)
so bug or not bug ?
Posted: Fri Mar 30, 2007 4:24 pm
by AND51
...that's the question!
No matter if this is really a bug, I#d like to see #PB_Macro in the next version.
Posted: Fri Mar 30, 2007 5:58 pm
by freak
Flype: you are still checking if the constant #InStr exists, not the Macro.
(for this reason, you always get a false in your code, even if the macro allready is defined)
...just renaming the constant does not give you a new compiler feature
Macros are resolved before compiler directives, which means that by the time
Defined() is evaluated, there is no more macro... it has been replaced by its content
This is the reason why (for now) there will be no such feature.
Posted: Fri Mar 30, 2007 6:29 pm
by Flype
ok freak, thank you for this clear explanation.
Posted: Fri Mar 30, 2007 7:41 pm
by AND51
However, defining a macro leads to compilation error insted of overriding a macro, for example.
So what should we do? Image the scenario, if you want to make you include public, everybody can use it but everybody must look at the code first to see if the macros are already defined.
So, if this feature is not planned yet, then there must be another way to solve this.
Posted: Fri Mar 30, 2007 7:44 pm
by Kaeru Gaman
give you Macros, Globals and Procedures in Public includes unique 3-4 letter prefixes...
...and provide a good documentation, this helps the user to avoid doubles.
Posted: Fri Mar 30, 2007 8:03 pm
by AND51
And what, if my include contains a macro called "GetArrayLength()"? Then the user cannot use this macro without modifing its name. Any reasons against this feature?
Posted: Fri Mar 30, 2007 8:12 pm
by Kaeru Gaman
AND51 wrote:And what, if my include contains a macro called "GetArrayLength()"?
then you forgot the prefix.
call it e.g. A51_GetArrayLength()
always give the content of your public includes unique prefixes!
Posted: Fri Mar 30, 2007 8:18 pm
by AND51
In the case of "GetArrayLength()" the purpose is, that the coder can use my macro, too! So why should I use a prefix then? To make the coder have more to write?
Posted: Fri Mar 30, 2007 8:24 pm
by Kaeru Gaman
....I somehow feel like pissing in a volcano....
the reason is, to make your MacroName unique and prevent problems.
what speaks against using prefixes?
have a look at the PBOSL or at ts' IncludePack...
Posted: Fri Mar 30, 2007 9:25 pm
by Flype
to be constructive, because freak explained it's not possible,
another solution could be the one i described here ( about an undefine command ) :
http://www.purebasic.fr/english/viewtopic.php?t=21899
no response here but if this one is possible it could be a solution to undefine structures, macros, and redefine ours.
i know Trond isn't agree but... what's the opinion of freak ?
i presume that it could be implemented for structs, etc... all but not macros for the same reason he gave me for the Defined() command.
