Skip to content
38 changes: 38 additions & 0 deletions LanguageFeatures/Patterns/cast_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2022, 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.

/// @assertion castPattern ::= primaryPattern 'as' type
///
/// A cast pattern is similar to an extractor pattern in that it checks the
/// matched value against a given type. But where an extractor pattern is
/// refuted if the value doesn't have that type, a cast pattern throws. Like the
/// null-assert pattern, this lets you forcibly assert the expected type of some
/// destructured value.
///
/// @description Check some valid cast patterns
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/static_type_helper.dart";

main() {
(num, Object) r1 = (1, "s");
var (i as int, s as String) = r1;
i.expectStaticType<Exactly<int>>();
s.expectStaticType<Exactly<String>>();

(Record,) r2 = ((),);
var (rec1 as (),) = r2;
rec1.expectStaticType<Exactly<()>>();

({num n, Object o}) r3 = (n: 1, o: "s");
var (n: n as int, o: o as String) = r3;
n.expectStaticType<Exactly<int>>();
o.expectStaticType<Exactly<String>>();

({Record r}) r4 = (r: ());
var (r: rec2 as ()) = r4;
rec2.expectStaticType<Exactly<()>>();
}
61 changes: 61 additions & 0 deletions LanguageFeatures/Patterns/cast_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2022, 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.

/// @assertion castPattern ::= primaryPattern 'as' type
///
/// A cast pattern is similar to an extractor pattern in that it checks the
/// matched value against a given type. But where an extractor pattern is
/// refuted if the value doesn't have that type, a cast pattern throws. Like the
/// null-assert pattern, this lets you forcibly assert the expected type of some
/// destructured value.
///
/// @description Check some valid cast patterns in a switch expressions and
/// statements
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

const Object zero = 0;

bool isZero(num i) {
return switch (i) {
zero as int => true,
_ => false
};
}

bool isInt(Object o) =>
switch (o) {
_ as int => true,
_ => false
};

main() {
Expect.isTrue(isZero(0));
Expect.isFalse(isZero(1));
Expect.isFalse(isZero(-1));
Expect.isTrue(isInt(42));
Expect.throws(() {
isInt("42");
});

int i = 0;
switch (i) {
case zero as int:
Expect.equals(0, i);
break;
default:
Expect.fail("Shouldn't be here");
}
Object j = 2;
switch (j) {
case var v as int:
Expect.equals(2, v);
break;
default:
Expect.fail("Shouldn't be here");
}
}
36 changes: 36 additions & 0 deletions LanguageFeatures/Patterns/cast_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022, 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.

/// @assertion castPattern ::= primaryPattern 'as' type
///
/// A cast pattern is similar to an extractor pattern in that it checks the
/// matched value against a given type. But where an extractor pattern is
/// refuted if the value doesn't have that type, a cast pattern throws. Like the
/// null-assert pattern, this lets you forcibly assert the expected type of some
/// destructured value.
///
/// @description Check some valid cast patterns in an if-case statements
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

const Object zero = 0;

main() {
int i = 0;
if (i case zero as int) {
Expect.equals(0, i);
} else {
Expect.fail("Shouldn't be here");
}
bool visited = false;
if (i case _ as int) {
visited = true;
} else {
Expect.fail("Shouldn't be here");
}
Expect.isTrue(visited);
}
41 changes: 41 additions & 0 deletions LanguageFeatures/Patterns/cast_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2022, 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.

/// @assertion castPattern ::= primaryPattern 'as' type
///
/// A cast pattern is similar to an extractor pattern in that it checks the
/// matched value against a given type. But where an extractor pattern is
/// refuted if the value doesn't have that type, a cast pattern throws. Like the
/// null-assert pattern, this lets you forcibly assert the expected type of some
/// destructured value.
///
/// @description Check that cast pattern throws if the value doesn't have an
/// expected type
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/expect.dart";

main() {
(num, Object) r1 = (1, 2);
Expect.throws(() {
var (i as int, s as String) = r1;
});

(Record,) r2 = ((3.14, name: "pi"),);
Expect.throws(() {
var (rec1 as (),) = r2;
});

({num n, Object o}) r3 = (n: 1, o: 2);
Expect.throws(() {
var (n: n as int, o: o as String) = r3;
});

({Record r}) r4 = (r: (1, 2));
Expect.throws(() {
var (r: rec1 as ()) = r4;
});
}
61 changes: 61 additions & 0 deletions LanguageFeatures/Patterns/cast_A02_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2022, 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.

/// @assertion castPattern ::= primaryPattern 'as' type
///
/// A cast pattern is similar to an extractor pattern in that it checks the
/// matched value against a given type. But where an extractor pattern is
/// refuted if the value doesn't have that type, a cast pattern throws. Like the
/// null-assert pattern, this lets you forcibly assert the expected type of some
/// destructured value.
///
/// @description Check that cast pattern throws if the value doesn't have an
/// expected type
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

bool isOdd1(Object o) =>
switch (o) {
var v as int => v.isOdd,
_ => false
};

bool isOdd2(Object o) {
switch (o) {
case var v as int:
return v.isOdd;
default:
return false;
}
}

bool isOdd3(Object o) {
if (o case var v as int) {
return v.isOdd;
}
return false;
}

main() {
Expect.isTrue(isOdd1(1));
Expect.isFalse(isOdd1(2));
Expect.throws(() {
isOdd1("1");
});

Expect.isTrue(isOdd2(1));
Expect.isFalse(isOdd2(2));
Expect.throws(() {
isOdd2("1");
});

Expect.isTrue(isOdd3(1));
Expect.isFalse(isOdd3(2));
Expect.throws(() {
isOdd3("1");
});
}
37 changes: 37 additions & 0 deletions LanguageFeatures/Patterns/null_assert_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022, 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.

/// @assertion nullAssertPattern ::= primaryPattern '!'
///
/// A null-assert pattern is similar to a null-check pattern in that it permits
/// non-null values to flow through. But a null-assert throws if the matched
/// value is null. It lets you forcibly assert that you know a value shouldn't
/// be null, much like the corresponding ! null-assert expression.
///
/// @description Check null-assert in a variable pattern
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/static_type_helper.dart";
import "../../Utils/expect.dart";

test1((int?, int?) position) {
var (x!, _!) = position;
x.expectStaticType<Exactly<int>>();
x.isOdd;
Expect.equals(1, x);
}

test2(({int? x, int? y}) position) {
var (x: x!, y: _!) = position;
x.expectStaticType<Exactly<int>>();
x.isOdd;
Expect.equals(1, x);
}

main() {
test1((1, 1));
test2((x: 1, y: 1));
}
41 changes: 41 additions & 0 deletions LanguageFeatures/Patterns/null_assert_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2022, 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.

/// @assertion nullAssertPattern ::= primaryPattern '!'
///
/// A null-assert pattern is similar to a null-check pattern in that it permits
/// non-null values to flow through. But a null-assert throws if the matched
/// value is null. It lets you forcibly assert that you know a value shouldn't
/// be null, much like the corresponding ! null-assert expression.
///
/// @description Check null-assert pattern in a switch statement
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

import "../../Utils/static_type_helper.dart";
import "../../Utils/expect.dart";

test(List<String?> list, [bool testVisited = false]) {
bool visited = false;
switch (list) {
case ['name', var name!]:
name.expectStaticType<Exactly<String>>();
name.substring(0);
break;
case ['answer', _!]:
visited = true;
break;
default:
Expect.fail("Wrong branch of code");
}
if (testVisited) {
Expect.isTrue(visited);
}
}

main() {
test(['name', 'Lily']);
test(['answer', '42'], true);
}
39 changes: 39 additions & 0 deletions LanguageFeatures/Patterns/null_assert_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2022, 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.

/// @assertion nullAssertPattern ::= primaryPattern '!'
///
/// A null-assert pattern is similar to a null-check pattern in that it permits
/// non-null values to flow through. But a null-assert throws if the matched
/// value is null. It lets you forcibly assert that you know a value shouldn't
/// be null, much like the corresponding ! null-assert expression.
///
/// @description Check null-assert pattern in an if-case statement
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

import "../../Utils/static_type_helper.dart";
import "../../Utils/expect.dart";

test(List<String?> list, [bool testVisited = false]) {
bool visited = false;
if (list case ['name', var name!]) {
name.expectStaticType<Exactly<String>>();
name.substring(0);
visited = true;
} else if (list case ['answer', _!]) {
visited = true;
} else {
Expect.fail("Wrong branch of code");
}
if (testVisited) {
Expect.isTrue(visited);
}
}

main() {
test(['name', 'Lily']);
test(['answer', '42'], true);
}
30 changes: 30 additions & 0 deletions LanguageFeatures/Patterns/null_assert_A01_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2022, 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.

/// @assertion nullAssertPattern ::= primaryPattern '!'
///
/// A null-assert pattern is similar to a null-check pattern in that it permits
/// non-null values to flow through. But a null-assert throws if the matched
/// value is null. It lets you forcibly assert that you know a value shouldn't
/// be null, much like the corresponding ! null-assert expression.
///
/// @description Check null-assert pattern in a switch expression
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

String test(List<String?> list) {
return switch (list) {
['name', var name!] => name.substring(0),
['answer', _!] => "answer",
_ => "default"
};
}

main() {
Expect.equals("Lily", test(['name', 'Lily']));
Expect.equals("answer", test(['answer', '42']));
}
Loading