Enhanced enumerations
Posted: Sun Oct 24, 2021 1:34 pm
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:
For now, I’m using modules to sort my constants, as I don’t find #PREFIXES_ very readable.
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
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.