Skip to content

Simplify enums #1104

@heaths

Description

@heaths

While reviewing a Cadl for a service team today, I was surprised by the verbosity of extensible enums:

enum ThingKindValues {
  A;
  B;
}
@knownValues(ThingKindValues)
model ThingKind is string {};
model Thing {
  kind: ThingKind;
}

Seems that could be simplified, but how really depends on whether Cadl wants to follow Azure guidelines by default or not (seems not).

If we say that enums are extensible by default, for example, we could simply declare the enum as, say, @fixed and just use it:

@fixed
enum ThingKind {
  A;
  B;
}
model Thing {
  kind: ThingKind;
}

Conversely, if enums are fixed by default, we could come up with something like @extensible and treat them, effectively as is string {} now.

The syntax is more concise for authors, and this also seems like less guesswork for generators. Take the C# generator, since .NET doesn't support unions nor string-based enums - either of which other languages do: we have to now look at two different models to determine if something should be a regular, fixed enum or an enum-like readonly struct per our current guidelines.

Metadata

Metadata

Assignees

Labels

design:neededA design request has been raised that needs a proposal

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions