Page 1 of 1

Enhanced enumerations

Posted: Sun Oct 24, 2021 1:34 pm
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.

Re: Enhanced enumerations

Posted: Fri Nov 12, 2021 1:56 pm
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.