dart-lang / dart-lang/language

Add a compile-time constant for platform

Open
#1,889 17 comments 29 reactions 0 assignees View on GitHub
enhanced-const feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Currently there are few ways to check for the platform. They are mostly runtime and have downsides. Ways to check for different platforms are inconsistent.

- `kIsWeb` of `flutter/foundation` is the most accepted way to check if the code is built for web. It translates to `const bool kIsWeb = identical(0, 0.0);` which is not future-proof. Who knows what other platforms Dart may support in the future that do not tell int from double?
- For other platforms we have fields like `Platform.isAndroid` of `dart:io` which is not supported in web. These translate to `static final bool isAndroid = (_operatingSystem == "android");` They take runtime comparison of strings which is not a first idea of efficiency when something is supposed to never change.

A cleaner and faster solution would be to have one constant for a platform so it could be used in a `switch` so that a compiler could drop unneeded branches for every platform.

Dart uses different compilers for different platforms, doesn't it? So it should be possible?

Contributor guide

Open the contributing guide

Research direction

Start by comparing the existing kIsWeb and dart:io Platform.isAndroid checks described in the issue, then review how Dart's different compilers could expose a compile-time platform value. No file or test is named; the work is complete only when the language-design and compiler implications are specified and accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.