Enumeration in Modules

Everything else that doesn't fall into one of the other PB categories.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Enumeration in Modules

Post by bbanelli »

Greetings to all, here's my code:

Code: Select all

DeclareModule Cars
  Global.i LastEnumeration = 0
EndDeclareModule

Module Cars 
EndModule

DeclareModule Ferrari
  UseModule Cars
  Debug LastEnumeration
  Enumeration LastEnumeration
    #a
    #b
    #c
  EndEnumeration
EndDeclareModule

Module Ferrari
  LastEnumeration + #PB_Compiler_EnumerationValue
  Debug "Ferrari " + Str(LastEnumeration)
EndModule

DeclareModule Porche
  UseModule Cars
  Debug LastEnumeration
  Enumeration LastEnumeration
    #a
    #b
    #c
  EndEnumeration
EndDeclareModule

Module Porche
  LastEnumeration + #PB_Compiler_EnumerationValue
  Debug "Porche " + Str(LastEnumeration)
EndModule

Debug Ferrari::#a
Debug Porche::#c
I was expecting Porche::#c to be 5, but it is 2 nevertheless. What am I doing wrong? I am trying to use this feature for a project where I'd avoid global "Enumeration" file, so I can simply input new enumerations in each separate file without any issues.

I've (sort of, since I'd like to use :: syntax for my enumerations, which would be super fantastic for code readability!) here -> http://www.forums.purebasic.com/english ... 87#p424695 but I'm still wondering what's wrong with my code.

BTW, if i "hardcode" a number after enumeration, it works as expected. So I guess this has nothing to do with modules but with a fact you can't assign variable to enumeration, right?

:punishing myself for being stupid:

Code: Select all

DeclareModule Ferrari
  Enumeration #PB_Compiler_EnumerationValue
    #a
    #b
    #c
  EndEnumeration
EndDeclareModule

Module Ferrari : EndModule

DeclareModule Porche
  Enumeration #PB_Compiler_EnumerationValue
    #a
    #b
    #c
  EndEnumeration
EndDeclareModule

Module Porche : EndModule

Debug Ferrari::#a
Debug Porche::#c
#PB_Compiler_EnumerationValue solved my problem, as it seems, by putting it directly after Enumeration... Do'h!

Image
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Enumeration in Modules

Post by Demivec »

A better solution would be to use a named enumeration in the Cars module. You are trying to duplicate this feature by using th variable LastEnumeration. I'll have time to post an example in a few hours but someone will probably beat me to it. :)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Enumeration in Modules

Post by ts-soft »

Code: Select all

DeclareModule Cars
  Enumeration Cars
  EndEnumeration
EndDeclareModule

Module Cars
EndModule

DeclareModule Ferrari
  UseModule Cars
  Enumeration Cars
    #ferrari_a
    #ferrari_b
    #ferrari_c
  EndEnumeration
EndDeclareModule

Module Ferrari
  Debug "Ferrari " + Str(#ferrari_c)
EndModule

DeclareModule Porche
  UseModule Cars
  Enumeration Cars
    #porsche_a
    #porsche_b
    #porsche_c
  EndEnumeration
EndDeclareModule

Module Porche
  Debug "Porche " + Str(#porsche_c)
EndModule

Debug Ferrari::#ferrari_a
Debug Porche::#porsche_c 
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
TI-994A
Addict
Addict
Posts: 2771
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Enumeration in Modules

Post by TI-994A »

bbanelli wrote:...I'd avoid global "Enumeration" file, so I can simply input new enumerations in each separate file without any issues.
The #PB_Compiler_EnumerationValue directive is great for regular enumerations, but misses the mark when enumerating different object types.

The best practice for such a model would be the use of common enumeration modules with named enumerators.

These posts might explain better:

Enumerating different objects separately.

Implementing common enumeration modules.

Hope it helps. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Enumeration in Modules

Post by bbanelli »

Demivec, ts-soft, TI-994A - thank you all for your feedback.

I was missing part where enumeration had to start in the first, "global" module. Now it works like a charm and has reduced complexity of my code by dramatically increasing readability with creating "namespaces" like this!
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
Post Reply