Page 1 of 1

Enumeration in Modules

Posted: Fri Aug 26, 2016 4:57 pm
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

Re: Enumeration in Modules

Posted: Fri Aug 26, 2016 6:50 pm
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. :)

Re: Enumeration in Modules

Posted: Fri Aug 26, 2016 7:09 pm
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 

Re: Enumeration in Modules

Posted: Sat Aug 27, 2016 4:41 am
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:

Re: Enumeration in Modules

Posted: Sun Aug 28, 2016 8:54 am
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!