Page 1 of 1
Enums as a Type
Posted: Sat May 17, 2025 11:22 am
by Axolotl
Enums as a Type
Code: Select all
Enumeration Action
Idle
Waiting
Copying
Analyzing
EndEnumeration
Procedure DoTheJob(Task.Action)
Select Task
Case Idle
; tbd.
Case Waiting
; tbd.
Case Copying
; tbd.
Case Analyzing
; tbd.
EndSelect
EndProcedure
Re: Enums as a Type
Posted: Sat May 17, 2025 11:53 am
by Demivec
Would that be simply be for type checking (of values)?
If it was implemented for type checking I think it would make more sense if it was written as follows:
Code: Select all
Enumeration Action
#Idle
#Waiting
#Copying
#Analyzing
EndEnumeration
Procedure DoTheJob(Enumer Task.Action)
Select Task
Case #Idle
; tbd.
Case #Waiting
; tbd.
Case #Copying
; tbd.
Case #Analyzing
; tbd.
EndSelect
EndProcedure
DoTheJob(#Idle)
Re: Enums as a Type
Posted: Sat May 17, 2025 12:45 pm
by User_Russian
I would also like a minimum and maximum function for enumeration.
Code: Select all
Enumeration Action
#Idle = 2
#Waiting
#Copying
#Analyzing
EndEnumeration
Debug EnumMinimum(Action) ; 2
Debug EnumMaximum(Action) ; 5
Re: Enums as a Type
Posted: Sat May 17, 2025 1:21 pm
by Quin
+1, I've wanted this forever, although I prefer the second approach shown myself

Re: Enums as a Type
Posted: Sat May 17, 2025 3:44 pm
by skywalk
What problem is this solving?
You want the IDE autocomplete to limit the enumerations while typing the case statements?
I solve this by naming ENUMs with a prefix.
StringFunction_DateTime_blahblah.
MIN and MAX are up to you?
Maybe if you are counting gadgets?
Code: Select all
Enumeration SF_DATETIMES
#SF_DT_MIN
#SF_DT_YYMMDD_HHMMSS
#SF_DT_YYMMDD_HHMMSS_MSS
#SF_DT_YYMMDD_HHMMSSMSS
#SF_DT_YYMMDDHHMMSSMSS
#SF_DT_YYMMDDHHMMSS
#SF_DT_YYMMDD
#SF_DT_HHMMSSDOTMSS
#SF_DT_HHMMSS
#SF_DT_HHMMSSMSS_COLON
#SF_DT_MAX
EndEnumeration
Re: Enums as a Type
Posted: Sat May 17, 2025 3:50 pm
by Axolotl
User_Russian wrote: Sat May 17, 2025 12:45 pm
I would also like a minimum and maximum function for enumeration.
Code: Select all
Enumeration Action
#Idle = 2
#Waiting
#Copying
#Analyzing
EndEnumeration
Debug EnumMinimum(Action) ; 2
Debug EnumMaximum(Action) ; 5
Well, I requested that (I called it Low() and HIgh()) in a differnt post?
Re: Enums as a Type
Posted: Sat May 17, 2025 3:55 pm
by User_Russian
The result is incorrect. It must be.
Code: Select all
Enumeration SF_DATETIMES
#SF_DT_MIN = 2
#SF_DT_YYMMDD_HHMMSS = #SF_DT_MIN
#SF_DT_YYMMDD_HHMMSS_MSS
#SF_DT_YYMMDD_HHMMSSMSS
#SF_DT_YYMMDDHHMMSSMSS
#SF_DT_YYMMDDHHMMSS
#SF_DT_YYMMDD
#SF_DT_HHMMSSDOTMSS
#SF_DT_HHMMSS
#SF_DT_HHMMSSMSS_COLON
#SF_DT_MAX = #SF_DT_HHMMSSMSS_COLON
EndEnumeration
If you need to add constants before #SF_DT_YYMMDD_HHMMSS or after #SF_DT_HHMMSSMSS_COLON, you will need to change #SF_DT_MIN and/or #SF_DT_MAX.
Re: Enums as a Type
Posted: Sat May 17, 2025 3:58 pm
by skywalk
Correct.
I was just asking why the current way is not good enough while also self-documenting?
Re: Enums as a Type
Posted: Sat May 17, 2025 4:00 pm
by Axolotl
skywalk wrote: Sat May 17, 2025 3:44 pm
What problem is this solving?
My request is about a more strict type checking by the compiler, from my POV it is not (only) a autocomplete function.
To the point User_Russian is mentioned I did a different request
here
Re: Enums as a Type
Posted: Sat May 17, 2025 4:32 pm
by Quin
skywalk wrote: Sat May 17, 2025 3:44 pm
What problem is this solving?
The problem this is solving is limiting your enumerations to only their defined values. If I have a window enum that goes from 0 to 5, if my Window enum was treated as a type, I could pass only a valid window, not some random arbitrary integer that would crash the program.
Re: Enums as a Type
Posted: Sat May 17, 2025 5:08 pm
by skywalk
But with standard types, there is overflow. You want PB to do different checks for typed ENUMs only?
Meaning, your code could still pass large enum values to your logic without compiler error?
Code: Select all
Define.a aa, ab
aa = 255
ab + aa + 256
Debug Str(ab)
Re: Enums as a Type
Posted: Sat May 17, 2025 7:15 pm
by Quin
What?
What does integer overflow have to do with anything?
There would be no possibility for overflow, because the allowed values would be limited to only the values in your enum...

Re: Enums as a Type
Posted: Sat May 17, 2025 8:50 pm
by #NULL
While it would be nice, and other languages have that, it is far away. PB doesn't even have type checking for structure parameters, which you declare and pass as pointers and you can pass just any number, not even just integers then, and structures are already types, kind of. Compile-time checking for enums isn't just a check at the call, but involves the whole type system. What if you want to store your current action in a variable. It couldn't be just an integer variable, but would have to be of the enum type. With this comes other questions like arithmetics/operators, for example increasing that variable to get the next action / enum value. Type conversions etc..
If you need unique values, i would use addresses, for example of variables in a module:
- no compile time checks
- using an empty structure to have a type for parameter declaration
- using duplicates of the action name (variables) and convert to address inside module, so you don't have to use Action::@Copy
Code: Select all
EnableExplicit
; ----------------------------------------
; Action Types
DeclareModule Action
Structure ActionType
EndStructure
Define Idle
Define Copy
EndDeclareModule
Module Action
Define _Idle
Define _Copy
Idle = @_Idle
Copy = @_Copy
EndModule
; ----------------------------------------
Procedure act(*a.Action::ActionType)
Select *a
Case Action::Idle
Debug "idle"
Case Action::Copy
Debug "copy"
Default
Debug "unkown"
EndSelect
EndProcedure
act(Action::Idle)
act(Action::Copy)
Define number.b
act(@number)
Define myAction = Action::Copy
act(myAction)
This isn't really different from just using enums directly in terms of types, but the values are less likely to come from another context like other/wrong enums or uninitialized variable etc. so it's less likely you pass a wrong value that works by accident.