icerpc / slicec

The Slice compiler library
Apache License 2.0
13 stars 5 forks source link

Add Support for Enums with Associated Fields #664

Closed InsertCreativityHere closed 10 months ago

InsertCreativityHere commented 10 months ago

This PR adds support for enumerators with associated fields.

This feature is only available in Slice2 mode, and only for enums that don't specify an underlying type.

enum E {
    A
    B(b: bool)
    C(s: Sequence<int32?>, tag(12) s: string)
}

The syntax of associated fields is equivalent to... fields (go figure). Anything supported by fields is supported by these.

Note that I am aware of the TODO in the cycle checking logic. I'm just splitting it off into it's own PR.

Summary of enums

In Slice1, enums cannot specify an underlying type, and enumerators cannot declare associated fields. Enumerators can declare values that are within the range of 0 and int32::MAX. If a 'Slice1' enum is used in a Slice2 file, it is treated as-if it has the underlying type int32.

In Slice2, enums can specify an underlying type. Enums with underlying types cannot use associated fields, but can assign values to it's enumerators. Enums without underlying types can use associated fields, but cannot assign values to it's enumerators.


See #30