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

ffigen should return enums, not ints #1201

Closed
Levi-Lesches opened this issue Jun 10, 2024 · 0 comments · Fixed by #1202
Closed

ffigen should return enums, not ints #1201

Levi-Lesches opened this issue Jun 10, 2024 · 0 comments · Fixed by #1202

Comments

@Levi-Lesches
Copy link
Contributor

Levi-Lesches commented Jun 10, 2024

Related to #446. Given the following C code:

typedef enum {
  ok = 1,
  error = 2,
} Status;

Status getStatus() { return ok; }

int getValue(Status status) {
  if (status == ok) return 1;
  else return 2;
}

ffigen will generate the following Dart code:

enum Status {
  ok(1),
  error(2);

  final int value;
  const Status(this.value);
}

// ...

int getStatus() {
  return _getStatus();
}

int getValue(int status) {
  return _getValue(status);
}

So it's still returning an integer. I think it should instead generate something like:

enum Status {
  // ...
  static Status fromValue(int value) => switch (value) {
    1 => Status.ok,
    2 => Status.error,
    _ => throw ArgumentError("Invalid value for Status: $value"),
  };
}

// ...

Status getStatus() {
  return Status.fromValue(_getStatus());
}

int getValue(Status status) {
  return _getValue(status.value);
}

cc @dcharkes and @eernstg from the previous discussions. PR at #1202

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant