c6347389d2
This moves pattern variable assignments of fully matched expressions into the case body. This will help backends (dart2js in particular) to reason about the code flow. Closes #54115 Change-Id: I598a384a829f16e91ab2d6a309499cf20b9cc121 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/339122 Commit-Queue: Johnni Winther <johnniwinther@google.com> Reviewed-by: Jens Johansen <jensj@google.com>
66 lines
1.5 KiB
Dart
66 lines
1.5 KiB
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.
|
|
|
|
// Derived from co19/src/LanguageFeatures/Patterns/invocation_keys_A04_t03.dart
|
|
|
|
import 'dart:collection';
|
|
|
|
class MyList<E> with ListMixin<E> {
|
|
final List<E> list;
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
MyList(this.list);
|
|
|
|
E operator [](int index) {
|
|
sb.write('[$index];');
|
|
return list[index];
|
|
}
|
|
|
|
void operator []=(int index, E value) {
|
|
list[index] = value;
|
|
}
|
|
|
|
int get length => list.length;
|
|
|
|
void set length(int value) {
|
|
list.length = value;
|
|
}
|
|
|
|
String get log => sb.toString();
|
|
|
|
void clearLog() {
|
|
sb.clear();
|
|
}
|
|
}
|
|
|
|
String test1(Object o) =>
|
|
switch (o) { [var x, 2, var y] => "match-1", _ => "no match" };
|
|
|
|
String test2(Object o) =>
|
|
switch (o) { [1, var x, var y] => "match-1", _ => "no match" };
|
|
|
|
String test3(Object o) => switch (o) {
|
|
[var x!, 1] => "match-1",
|
|
[1, var x!] => "match-2",
|
|
_ => "no match"
|
|
};
|
|
|
|
main() {
|
|
final ml1 = MyList<int>([1, 2, 3]);
|
|
expect("match-1", test1(ml1));
|
|
expect("[0];[1];[2];", ml1.log);
|
|
|
|
final ml2 = MyList<int>([1, 2, 3]);
|
|
expect("match-1", test2(ml2));
|
|
expect("[0];[1];[2];", ml2.log);
|
|
|
|
final ml3 = MyList<int>([1, 2]);
|
|
expect("match-2", test3(ml3));
|
|
expect("[0];[1];", ml3.log);
|
|
}
|
|
|
|
expect(expected, actual) {
|
|
if (expected != actual) throw 'Expected $expected, actual $actual';
|
|
}
|