Enums as a Type

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Enums as a Type

Post 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 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
Demivec
Addict
Addict
Posts: 4267
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Enums as a Type

Post 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)
User_Russian
Addict
Addict
Posts: 1525
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Enums as a Type

Post 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
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Enums as a Type

Post by Quin »

+1, I've wanted this forever, although I prefer the second approach shown myself :D
User avatar
skywalk
Addict
Addict
Posts: 4215
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Enums as a Type

Post 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
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Enums as a Type

Post 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?
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User_Russian
Addict
Addict
Posts: 1525
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Enums as a Type

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 4215
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Enums as a Type

Post by skywalk »

Correct.
I was just asking why the current way is not good enough while also self-documenting?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Enums as a Type

Post 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
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Enums as a Type

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 4215
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Enums as a Type

Post 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)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Enums as a Type

Post 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...
:?:
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Enums as a Type

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