-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
JS: Add a new "undefined" type (or proxy it as "void")? #31515
Comments
One of the reasons dart2js and DDC treat both JavaScript There are two values that are Dart If you want |
I mean we can be (clever?) here, most of the time (99%) you don't care. I'd be fine if it "seemed" like null to Dart2JS/DDC, but there was a way of telling what it is: library js;
external bool isUndefined(Null nullOrUndefined); Example use: import 'package:js/js.dart' as js;
void callWithArg(dynamic arg) {
if (arg == null) {
if (js.isUndefined(arg)) {
// This is actually, at runtime, `undefined`, not `null`.
} else {
// This is actually, at runtime, `null`.
}
}
} Would that be OK? I'd still need a way to assign undefined: library js;
/// Returns the JS "undefined" type, i.e. equivalent to `void 0`.
external Null get undefined; import 'package:js/js.dart' as js;
void useJsInterop(ChartJs chart) {
chart.someApi(js.undefined);
} |
Add a default value and try/catch block around WheelEvent.delta(X/Y/Mode). html_dart2js does not throw error when deltaX/deltaY is null. So null handling is also added. Add test for null deltaX/deltaY/deltaMode because 'undefined' cannot be passed as value. dart-lang/sdk#24088 dart-lang/sdk#31515 PiperOrigin-RevId: 218253658
Add a default value and try/catch block around WheelEvent.delta(X/Y/Mode). html_dart2js does not throw error when deltaX/deltaY is null. So null handling is also added. Add test for null deltaX/deltaY/deltaMode because 'undefined' cannot be passed as value. dart-lang/sdk#24088 dart-lang/sdk#31515 PiperOrigin-RevId: 218253658
Adding insult to the injury here regarding @rakudrama's comment, let a = new Array(10);
a.forEach((x) => {
console.log('foo');
});
for (let i = 0; i < a.length; i++) {
console.log('bar');
} The above code prints "bar" 10 times but no "foo". The |
Would supercede #24088.
In JavaScript some APIs have a distinction between
undefined
andnull
, i.e. they do:This isn't terribly different from how we treat optional parameters today, i.e. the call site determines if it uses the default expression, if any, otherwise it is
null
. Unfortunately we lack the way to:undefined
JS type (for JS-interop) - see Need a way of passingundefined
to JS via dart:js #24088.undefined
type ourselves as a type of (pun intended) sentinel value.The second use case is extra interesting, because some JavaScript VM's can understand that
null
andundefined
are sentinel-like values, but they can't tell that some arbitrary Dart object is (believing it is polymorphic) - but more importantly, because we'd like to retain type inference and types wherever possible.This works, OK (minus potential perf consequences of using an arbitrary object), but notice that I am forced to use a
dynamic
type for practically everything. In Dart 2, I might be able to "tighten" this up a bit:If I had an
undefined
type (or could usevoid
for this purpose):It gets a little tricky with non-nullability, but again, when you're dealing with JS APIs you don't have the luxury of pretending everything is 100% Dart. This would be no less "bad" than writing the following code yourself:
The text was updated successfully, but these errors were encountered: