Skip to content

withResource() when callback returns a future and strong mode errors #1550

Description

@adamlofts

The following code runs fine:

import 'package:pool/pool.dart';

main() async {
  Pool pool = new Pool(1);
  String s = await pool.withResource(() async => "Hi");
  print(s);
}

However in strong mode I get the following errors:

dartanalyzer --strong bin/testpool.dart
Analyzing [bin/testpool.dart]...
[error] Couldn't infer type parameter 'T'; 'Future' must be of type 'String'. (/home/adam/dev/fp3/dart/yacht/bin/testpool.dart, line 6, col 25)
[error] The argument type '() → Future' can't be assigned to the parameter type '() → String'. (/home/adam/dev/fp3/dart/yacht/bin/testpool.dart, line 6, col 38)
2 errors found.

I believe this is because withResource allows callback to return a T or Future<T> but this is not reflected in the function signature. Probably the type of callback should be: FutureOr<T> callback().

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions