Skip to content

NS_ENUM does not allow integer assignment #1217

Closed
@brianquinlan

Description

The NS_ENUM macro allows for assignment of enum values other than those declared.

But the Objective-C ffigen does not allow that. For example:

typedef NS_ENUM(NSInteger, NSURLSessionWebSocketCloseCode)
{
    NSURLSessionWebSocketCloseCodeInvalid =                             0,
    NSURLSessionWebSocketCloseCodeNormalClosure =                    1000,
    NSURLSessionWebSocketCloseCodeGoingAway =                        1001,
    NSURLSessionWebSocketCloseCodeProtocolError =                    1002,
    NSURLSessionWebSocketCloseCodeUnsupportedData =                  1003,
    NSURLSessionWebSocketCloseCodeNoStatusReceived =                 1005,
    NSURLSessionWebSocketCloseCodeAbnormalClosure =                  1006,
    NSURLSessionWebSocketCloseCodeInvalidFramePayloadData =          1007,
    NSURLSessionWebSocketCloseCodePolicyViolation =                  1008,
    NSURLSessionWebSocketCloseCodeMessageTooBig =                    1009,
    NSURLSessionWebSocketCloseCodeMandatoryExtensionMissing =        1010,
    NSURLSessionWebSocketCloseCodeInternalServerError =              1011,
    NSURLSessionWebSocketCloseCodeTLSHandshakeFailure =              1015,
} API_AVAILABLE(macos(10.15), ios(13.0), watchos(6.0), tvos(13.0));

to

enum NSURLSessionWebSocketCloseCode {
  NSURLSessionWebSocketCloseCodeInvalid(0),
  NSURLSessionWebSocketCloseCodeNormalClosure(1000),
  NSURLSessionWebSocketCloseCodeGoingAway(1001),
  NSURLSessionWebSocketCloseCodeProtocolError(1002),
  NSURLSessionWebSocketCloseCodeUnsupportedData(1003),
  NSURLSessionWebSocketCloseCodeNoStatusReceived(1005),
  NSURLSessionWebSocketCloseCodeAbnormalClosure(1006),
  NSURLSessionWebSocketCloseCodeInvalidFramePayloadData(1007),
  NSURLSessionWebSocketCloseCodePolicyViolation(1008),
  NSURLSessionWebSocketCloseCodeMessageTooBig(1009),
  NSURLSessionWebSocketCloseCodeMandatoryExtensionMissing(1010),
  NSURLSessionWebSocketCloseCodeInternalServerError(1011),
  NSURLSessionWebSocketCloseCodeTLSHandshakeFailure(1015);

  final int value;
  const NSURLSessionWebSocketCloseCode(this.value);

  static NSURLSessionWebSocketCloseCode fromValue(int value) => switch (value) {
        0 => NSURLSessionWebSocketCloseCodeInvalid,
        1000 => NSURLSessionWebSocketCloseCodeNormalClosure,
        1001 => NSURLSessionWebSocketCloseCodeGoingAway,
        1002 => NSURLSessionWebSocketCloseCodeProtocolError,
        1003 => NSURLSessionWebSocketCloseCodeUnsupportedData,
        1005 => NSURLSessionWebSocketCloseCodeNoStatusReceived,
        1006 => NSURLSessionWebSocketCloseCodeAbnormalClosure,
        1007 => NSURLSessionWebSocketCloseCodeInvalidFramePayloadData,
        1008 => NSURLSessionWebSocketCloseCodePolicyViolation,
        1009 => NSURLSessionWebSocketCloseCodeMessageTooBig,
        1010 => NSURLSessionWebSocketCloseCodeMandatoryExtensionMissing,
        1011 => NSURLSessionWebSocketCloseCodeInternalServerError,
        1015 => NSURLSessionWebSocketCloseCodeTLSHandshakeFailure,
        _ => throw ArgumentError(
            "Unknown value for NSURLSessionWebSocketCloseCode: $value"),
      };
}

In Objective-C, it would be possible to use your own close code (e.g. 3000), but this is not possible in ffigened Objective-C APIs.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions