90bba41618
`o is T` where `T` is an interface implemented by a class hierarchy can be implemented as `o instanceof R` where `R` is the root of the hierarchy. The common case is that `R` is a single implementation class. `is Record` and `is Type` now generally use `instanceof`. Fixes: #51366 Change-Id: I1943533bc53024f3199f71910034d49cf6661b22 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/294320 Commit-Queue: Stephen Adams <sra@google.com> Reviewed-by: Mayank Patke <fishythefish@google.com>
49 lines
1012 B
Dart
49 lines
1012 B
Dart
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
// `is` tests against some interface types are compiled to `instanceof` tests.
|
|
|
|
/*member: isRecord:function(o) {
|
|
return o instanceof A._Record;
|
|
}*/
|
|
isRecord(o) => o is Record;
|
|
|
|
/*member: isRegExp:function(o) {
|
|
return o instanceof A.JSSyntaxRegExp;
|
|
}*/
|
|
isRegExp(o) => o is RegExp;
|
|
|
|
/*member: isString:function(o) {
|
|
return typeof o == "string";
|
|
}*/
|
|
isString(o) => o is String;
|
|
|
|
/*member: isType:function(o) {
|
|
return o instanceof A._Type;
|
|
}*/
|
|
isType(o) => o is Type;
|
|
|
|
/*member: main:ignore*/
|
|
main() {
|
|
final items = [
|
|
1,
|
|
'x',
|
|
true,
|
|
Object(),
|
|
main,
|
|
Type,
|
|
(1, 2),
|
|
(1, 2, 3),
|
|
StringBuffer(),
|
|
RegExp('a'),
|
|
[1],
|
|
];
|
|
|
|
for (final item in items) {
|
|
for (final test in [isRecord, isRegExp, isString, isType]) {
|
|
print(test(item));
|
|
}
|
|
}
|
|
}
|