Enhanced enumerations

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Enhanced enumerations

Post by Joubarbe »

It seems to me that some improvements could be made to enumerations, such as implemented in Rust or TypeScript as custom types. It’s especially true when it comes to teamwork, when you want to impose some restrictions:

Code: Select all

Enumeration MyEnum
  #CONST1
  #CONST2
EndEnumeration

Define foo.MyEnum
foo = MyEnum\#CONST1 ; Autocomplete would show the two constants.
foo = 2 ; Raises an error, as MyEnum can only be 0 (#CONST1) or 1 (#CONST2).

Procedure FooFunc(int.MyEnum)
  ;int must be either 0 or 1.
EndProcedure
For now, I’m using modules to sort my constants, as I don’t find #PREFIXES_ very readable.

Code: Select all

DeclareModule EnumTest
  Enumeration
    #CONST3
    #CONST4
  EndEnumeration
EndDeclareModule
Module EnumTest : EndModule

bar.i = EnumTest::#CONST4
Debug bar ; Outputs 1, but "bar" can be anything.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Enhanced enumerations

Post by spikey »

What you propose might cause an ambiguity problem with defined structures but it seems a reasonable idea in view of the fact that freak has been thinking about context sensitivity for built-in constants in the IDE.

I was thinking about maybe an 'in' or 'from' keyword:

Code: Select all

Procedure FooFunc(int.i In MyEnum)
; or
Procedure FooFunc(int.i From MyEnum)
This wouldn't break existing code as In/From can be optional (would need to be in fact) and no ambiguities arise (that I can think of). It could be used by the IDE for filtering autocomplete information and the compiler for boundary checks. It would also increase the self-documentation of source code.
Post Reply