Hello, I've run into an issue on MacOS using the command line. I recently made some changes to my code around the same time as I upgraded flutter, so I spent several hours trying to debug this issue as a flutter issue before realizing that my change was actually the culprit and that this issue has nothing to do with Flutter.
I've managed to trace my problem; I refactored a switch expression into a switch statement to streamline some logic. Unfortunately, one of the conditions wasn't constant, but neither I nor the compiler managed to catch this error. See code below.
I have an extension that provides a >= operator, which I was using in the switch expression. This was working before. However, if I try to use that same extension in the switch statement, neither the analyzer nor compiler issue a warning or error. Instead, when I try to run the test, I get a pretty big, unhelpful error.
Instead, I would expect:
An error is surfaced by the analyzer on my non-constant switch case
In the error case, a more helpful error message to help me find the problem
That both the switch statement and the switch expression would function (or not function in) the same way. If one throws, I would expect the other to as well.
Test Code
import 'package:test/test.dart';
void main() {
test("Switch Expression", () {
final result = switch (MyEnum.first) {
>= MyEnum.second => "More than second",
MyEnum.first => "One value",
_ => "Some values",
};
expect(result, "One value");
});
// Causes an obtuse `InvalidArgument(s): Type parameter StructuralParameter(T) is not indexed`
// error. Comment out this test and the file will compile again.
test("Switch Statement", () {
String result;
switch (MyEnum.first) {
case >= MyEnum.second:
result = "More than second";
case MyEnum.first:
result = "One value";
default:
result = "Some values";
}
expect(result, "One value");
});
}
enum MyEnum implements Comparable<MyEnum> {
first(1),
second(2),
third(3);
const MyEnum(this.value);
final int value;
@override
int compareTo(MyEnum other) => value.compareTo(other.value);
// FIXME: Operator >= not implemented
// operator >=(MyEnum other) => value.compareTo(other.value) >= 0;
}
extension GreaterThanOrEqual<T> on Comparable<T> {
operator >=(T other) => compareTo(other) >= 0;
}
Summary: Using extensions in Dart switch statements with non-constant conditions causes unhelpful compiler errors. The analyzer should flag the issue and provide a more informative error message.
Hello, I've run into an issue on MacOS using the command line. I recently made some changes to my code around the same time as I upgraded flutter, so I spent several hours trying to debug this issue as a flutter issue before realizing that my change was actually the culprit and that this issue has nothing to do with Flutter.
I've managed to trace my problem; I refactored a switch expression into a switch statement to streamline some logic. Unfortunately, one of the conditions wasn't constant, but neither I nor the compiler managed to catch this error. See code below.
I have an extension that provides a
>= operator
, which I was using in the switch expression. This was working before. However, if I try to use that same extension in the switch statement, neither the analyzer nor compiler issue a warning or error. Instead, when I try to run the test, I get a pretty big, unhelpful error.Instead, I would expect:
Test Code
Compile-time Error
Dart Info
(Sorry, built the project with Flutter)