Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums as Types #26

Open
that-hatter opened this issue Apr 2, 2024 · 3 comments
Open

Enums as Types #26

that-hatter opened this issue Apr 2, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@that-hatter
Copy link
Member

that-hatter commented Apr 2, 2024

Should Enums be usable as Types? e.g.:

parameters:
  - name: loc
    type: [ Location ]
    description: The location to check.

instead of using int and linking to the enum in the description:

parameters:
  - name: loc
    type: [ int ]
    description: The [Location](/api/enums/Location) to check.

This means the signature of Card.IsLocation in the site would be displayed as Card.IsLocation(Card c, Location loc) instead of Card.IsLocation(Card c, int loc).

Pros:

  • The allowed values are formalized and summarized nicely. Can also help static analyzers narrow down erroneous values.
  • No need to redundantly link to the enum in the description, especially if the enum's name gets mentioned more than once. (Related: shorthand for linking to api entries)

Cons:

  • Could confuse people expecting an "enum" to represent all allowed values when used as a type. For example, in the case of the Location enum, you could actually pass a combination of LOCATION_ constants, e.g. LOCATION_MZONE|LOCATION_SZONE.
@that-hatter that-hatter added the enhancement New feature or request label Apr 2, 2024
@feeshstick
Copy link
Collaborator

feeshstick commented Apr 2, 2024

you can still display it as int. As all enum entries are Integer.
imo. they should be used as types.


if you want to go really fancy and intend to confuse users, do something like this

IntExpression<E, Op> ::= E | IntExpression<E, Op> Op E
type: [ IntExpression<Location, Op.Pipe | Op.Plus> ]

@that-hatter
Copy link
Member Author

I don't think it's a good idea to use enums as types then not display them as such. Sort of defeats the purpose.

@feeshstick
Copy link
Collaborator

I don't think it's a good idea to use enums as types then not display them as such. Sort of defeats the purpose.

I think it does make sense to display them as types which extend int to avoid your concern of confusion.
You could create a type / extend the enum to allow for certain operations like '+' or '|'
afaik, there's no enum that can be used in combination of a value defined in another enum.
but if there is, it could be defined as new type combining the enums with allowed operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants