-
Notifications
You must be signed in to change notification settings - Fork 214
If/else compiles however conditional operator does not #1904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The static type of a conditional expression is the Least Upper Bound of the static type of the two cases. The type of an expression returned should be assignable to the return type, but Object is not assignable to |
I moved this issue to the language repository and marked it with the label 'least-upper-bound', such that it is included in the group of issues on this topic area. |
At this time the proposal in #1618 has been implemented (that is In particular, the following code is accepted by the analyzer and the common front end with no errors, with // ----------------------------------------------------------------------
// Glue code, added by eernst.
import 'dart:convert';
class Result<X> {}
class ErrorResult extends Result<Never> {
final Object message;
ErrorResult(this.message);
}
class ValueResult<X> extends Result<X> {
final X value;
ValueResult(this.value);
}
class Response {
String get body => '';
int get statusCode => 42;
}
// ----------------------------------------------------------------------
// Code from the issue.
Future<Result<Map<String, dynamic>>> withResult1(
Future<Response> Function() f) async {
final response = await f();
late final Map<String, dynamic> body;
try {
body = jsonDecode(response.body);
} catch (e) {
return ErrorResult("Unknown error.");
}
if (response.statusCode == 200) {
return ValueResult(body);
} else {
return ErrorResult(body);
}
}
Future<Result<Map<String, dynamic>>> withResult2(
Future<Response> Function() f) async {
final response = await f();
late final Map<String, dynamic> body;
try {
body = jsonDecode(response.body);
} catch (e) {
return ErrorResult("Unknown error.");
}
return response.statusCode == 200 ? ValueResult(body) : ErrorResult(body);
}
void main() {} |
Hopefully this is in the right spot.
Version: Dart SDK version: 2.14.3 (stable) (Wed Sep 29 13:10:26 2021 +0200) on "windows_x64"
I am using Android Studio 3.6.3.
The following function works:
However when changing the final if/else to a conditional operator, it does not, and throws the error:
"A value of type 'Object' can't be returned from the function 'withResult' because it has a return type of 'Future<Result<Map<String, dynamic>>>'.
I would have thought these 2 operators to be functionally the same, should it be working like this? Thanks!
The text was updated successfully, but these errors were encountered: