dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.03k stars 1.55k forks source link

[library] Add an `OS` type and consts. #49144

Open dcharkes opened 2 years ago

dcharkes commented 2 years ago

We have a Platform.operatingSystem.

However, it's value as String does not lend itself well for programmatic use. (E.g. the type will always be String, which is very poor for documentation on functions.)

We should consider introducing a type representing an OS, a constants for the known values, a constant for all known values, and a current.

class OS {
  final String name;

  const OS._(this.name);

  static const OS android = OS._('android');
  static const OS fuchsia = OS._('fuchsia');
  static const OS ios = OS._('ios');
  static const OS linux = OS._('linux');
  static const OS macos = OS._('macos');
  static const OS windows = OS._('windows');

  // This could be a set, but then it's not ordered alphabetically.
  static const List<OS> knownValues = [
    android,
    fuchsia,
    ios,
    linux,
    macos,
    windows,
  ];

  // We'd prefer this to be const, which requires language features.
  external static OS get current;
}

Design similar to Abi in dart:ffi, but Abi is sealed this isn't.

OS should not be an enum, because if we would ever add an operating system it would break all exhaustive switches.

Having current as const requires language work:

Some open questions:

  1. Would it be useful to introduce OS (or OperatingSystem) without a non-const current until we have a way of making current const?
  2. How do we cleanup the API of Platform.operatingSystem returning a string? And possible other duplicate functionality between OS and Platform?
  3. Can we do anything about duplication between Flutter and Dart? https://api.flutter.dev/flutter/foundation/TargetPlatform.html

@lrhn I tried to summarize what you mentioned earlier. I didn't find an existing tracking issue yet. If we already have one please close this one.

dcharkes commented 2 years ago

Can we do anything about duplication between Flutter and Dart?

We have even more duplication.

Current list of alternatives:

brianquinlan commented 1 year ago

Why are Platform.isIOS not sufficient?

dcharkes commented 1 year ago

Why are Platform.isIOS not sufficient?

  1. It's useful to be able to pass objects around and make add a switch for example.
  2. Sometimes you might want to cross-compile or say something about a configuration for another target platform. This one is static, only for the current platform.
dcharkes commented 1 year ago

If the dart:io owners want this, I'm fine with the implementation.

@lrhn in https://dart-review.googlesource.com/c/sdk/+/275640

@a-siva does anyone dart:io? @brianquinlan do you own dart:io?

brianquinlan commented 1 year ago

@brianquinlan do you own dart:io?

Yes.