Is it possible to override a enum?

#1

Say I have the following type that uses ExampleEnum defined in package A:

entity type example schema name 'example' {
  field1: ~ enum ExampleEnum
}

enum type ExampleEnum {
  A,
  B
}

Then I have package B that depends on package A but now I want a different set of enums for package B:

enum type ExampleEnum {
  C,
  D
}

If I declare it like above, I get duplicate type declared. If I do a remix type, field 1 will be restricted to A, B, C, D instead of just C, D. Is there a way using the type system to do this besides creating a new enum for each package? Was hoping to use the same enum and if one is not defined, would use the default one that’s defined in the base package.

0 Likes

#2

Enums are part of a contract, so we generally don’t allow them to be changed. Imagine if you implemented a method that took an enum as an argument; your implementation could depend on a constrained set of values. Being able to re-defined the enum would break the implementation. (This is the same reason enums are not extendable in Java.)

We do allow remixing an enum to add new values, but you cannot remove existing values. It’s also somewhat questionable practice if the enum is used by code that depends on the possible values. (This mainly exists for enums that represent things like status codes which are open-ended.)

enum type Colors { RED, GREEN, BLUE }
remix enum type Colors { GRAY }
0 Likes