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

Generated Dart code doesn't allow combining NS_OPTIONS #1518

Open
stuartmorgan opened this issue Sep 5, 2024 · 1 comment
Open

Generated Dart code doesn't allow combining NS_OPTIONS #1518

stuartmorgan opened this issue Sep 5, 2024 · 1 comment
Labels
lang-objective_c Related to Objective C support package:ffigen

Comments

@stuartmorgan
Copy link

stuartmorgan commented Sep 5, 2024

I was attempting to migrate the following Obj-C code to Dart:

[item addObserver:observer
       forKeyPath:@"loadedTimeRanges"
          options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
          context:NULL];

Where the options are defined by:

typedef NS_OPTIONS(NSUInteger, NSKeyValueObservingOptions) {
    NSKeyValueObservingOptionNew = 0x01,
    NSKeyValueObservingOptionOld = 0x02,
    NSKeyValueObservingOptionInitial = 0x04,
    NSKeyValueObservingOptionPrior = 0x08

};

The generated code is:

enum NSKeyValueObservingOptions {
  NSKeyValueObservingOptionNew(1),
  NSKeyValueObservingOptionOld(2),
  NSKeyValueObservingOptionInitial(4),
  NSKeyValueObservingOptionPrior(8);

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

  static NSKeyValueObservingOptions fromValue(int value) => switch (value) {
        1 => NSKeyValueObservingOptionNew,
        2 => NSKeyValueObservingOptionOld,
        4 => NSKeyValueObservingOptionInitial,
        8 => NSKeyValueObservingOptionPrior,
        _ => throw ArgumentError(
            "Unknown value for NSKeyValueObservingOptions: $value"),
      };
}

which is essentially unusable:

  • First, it doesn't define operator |, so they can't be directly combined.

  • Second, and worse, that can't be worked around. Before I looked at the full implementation when I just got the error about |, I tried:

    NSKeyValueObservingOptions.fromValue(
              NSKeyValueObservingOptions.NSKeyValueObservingOptionInitial.value |
                  NSKeyValueObservingOptions.NSKeyValueObservingOptionNew.value)

    but that throws the ArgumentError at runtime because, of course, the combination of two values is not one of the values.

Since the whole point of NS_OPTIONS as distinct from NS_ENUM is to allow combining values, the generator needs to make different code for NS_OPTIONS.

@liamappelbe
Copy link
Contributor

Use the enums.as-int option to choose which enums to generate as int constants instead of Dart enums.

@liamappelbe liamappelbe added the lang-objective_c Related to Objective C support label Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang-objective_c Related to Objective C support package:ffigen
Projects
Status: Todo
Development

No branches or pull requests

2 participants