Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Conditional type annotation #77

Closed
Sunbreak opened this issue Feb 9, 2021 · 3 comments
Closed

Conditional type annotation #77

Sunbreak opened this issue Feb 9, 2021 · 3 comments

Comments

@Sunbreak
Copy link

Sunbreak commented Feb 9, 2021

Background

I'm working on Dart wrapper via dart:ffi for libusb: https://github.com/woodemi/libusb.dart

Problem

libusb_linux.dart is identical to libusb_windows.dart while libusb_mac.dart is a little different

  • Windows/Linux
class timeval extends ffi.Struct {
  // ...
  @ffi.Int64()
  int tv_usec;
}
  • macOS
class timeval extends ffi.Struct {
  // ...
  @ffi.Int32()
  int tv_usec;
}

Calling the APIs requires import 'packages:libusb/libusb_windows.dart for Windows, libusb_macos.dart for macOS and libusb_linux.dart for Linux

https://github.com/woodemi/libusb.dart/blob/afabb0fb3deeef240b6a893cf79a92cf1aa878f8/example/listdevs.dart#L7-L10

// git update-index --assume-unchanged example/listdevs.dart
// import 'package:libusb/libusb_windows.dart';
// import 'package:libusb/libusb_macos.dart';
// import 'package:libusb/libusb_linux.dart';

It's quite cumbersome and error prone

Feature Request

Could we support something like

class timeval extends ffi.Struct {
  // ...
  @ffi.Int64() if (Platforms.isMac) @ffi.Int32()
  int tv_usec;
}
@Sunbreak
Copy link
Author

Sunbreak commented Feb 9, 2021

BTW, Conditional Import via Platforms.isWindows/isMacOS/isLinux would work as well

import 'timeval64.dart if (Platforms.isMac) timeval32.dart';

However, Conditional Import is only available for Dart SDK library, i.e. dart.library.io/dart.library.js/dart.library.html/dart.library.ffi as far as I know

@dcharkes
Copy link
Contributor

dcharkes commented Feb 9, 2021

Yes, we are tracking this here: dart-lang/sdk#42563.

@dcharkes
Copy link
Contributor

dcharkes commented Feb 9, 2021

I'll go ahead and close this issue, as this is not a package:ffi issue, but rather a dart:ffi feature, and we're already tracking it.

@dcharkes dcharkes closed this as completed Feb 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants