|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +/// @assertion castPattern ::= primaryPattern 'as' type |
| 6 | +/// |
| 7 | +/// A cast pattern is similar to an extractor pattern in that it checks the |
| 8 | +/// matched value against a given type. But where an extractor pattern is |
| 9 | +/// refuted if the value doesn't have that type, a cast pattern throws. Like the |
| 10 | +/// null-assert pattern, this lets you forcibly assert the expected type of some |
| 11 | +/// destructured value. |
| 12 | +/// |
| 13 | +/// @description Check some valid cast patterns |
| 14 | + |
| 15 | +
|
| 16 | +// SharedOptions=--enable-experiment=patterns,records |
| 17 | + |
| 18 | +import "../../Utils/static_type_helper.dart"; |
| 19 | + |
| 20 | +main() { |
| 21 | + (num, Object) r1 = (1, "s"); |
| 22 | + var (i as int, s as String) = r1; |
| 23 | + i.expectStaticType<Exactly<int>>(); |
| 24 | + s.expectStaticType<Exactly<String>>(); |
| 25 | + |
| 26 | + (Record,) r2 = ((),); |
| 27 | + var (rec1 as (),) = r2; |
| 28 | + rec1.expectStaticType<Exactly<()>>(); |
| 29 | + |
| 30 | + ({num n, Object o}) r3 = (n: 1, o: "s"); |
| 31 | + var (n: n as int, o: o as String) = r3; |
| 32 | + n.expectStaticType<Exactly<int>>(); |
| 33 | + o.expectStaticType<Exactly<String>>(); |
| 34 | + |
| 35 | + ({Record r}) r4 = (r: ()); |
| 36 | + var (r: rec2 as ()) = r4; |
| 37 | + rec2.expectStaticType<Exactly<()>>(); |
| 38 | +} |
0 commit comments