Defined() and #PB_Macro

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Defined() and #PB_Macro

Post 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
Last edited by Flype on Tue Jan 09, 2007 1:37 pm, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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: :?:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

** bump ** (sorry)

so bug or not bug ?
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

...that's the question! :wink:


No matter if this is really a bug, I#d like to see #PB_Macro in the next version.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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 :P

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.
quidquid Latine dictum sit altum videtur
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

ok freak, thank you for this clear explanation.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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!
oh... and have a nice day.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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...
oh... and have a nice day.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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.

:?:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Post Reply