Skip to content

[Bug]: where parameter for contitional caching does not support generic functions #128

@vbrandl

Description

@vbrandl

Desctibe the bug

I have several API classes that all return a generic ApiResult<T> (see the code in the reproduction steps for definitions.

I only want to cache requests that were successful (e.g. internal server errors should not be cached)

Now I want to write a function to conditionally cache the results of my API calls.

To simplify the example, my functions return List<T> and my caching condition is that the list must not be empty.

This condition is generic over the actual type of the List, so I'd like to have a generic function to check if a result should be cached or not.

But if I define a generic function, the code generation fails:

E [ERROR] Parameter: result (of type List<T>) should match type Future<List<int>>.
  package:blub/model/api/temp.dart:6:14
    ╷
  6 │ Future<bool> _shouldCacheGeneric<T>(List<T> result) async => result.isNotEmpty;
    │              ^^^^^^^^^^^^^^^^^^^
    ╵

So I have to implement a separate conditional function for each type my API might return.

It would be nice, if generic functions were supported here.

Reproduction steps

Future<bool> _shouldCacheConcrete(List<int> result) => _shouldCacheGeneric(result);
Future<bool> _shouldCacheGeneric<T>(List<T> result) async => result.isNotEmpty;

@WithCache()
abstract mixin class SomeAPI implements _$SomeAPI {
  factory SomeAPI() => _SomeAPI();

  @Cached(syncWrite: true, limit: 1, where: _shouldCacheGeneric)
  Future<List<int>> getAll() async => [];
}
  1. Change the where parameter to _shouldCacheGeneric
  2. Run the code generation again

Expected behaviour

A generic function should be allowed for where

Dart version

3.8.1

Package version

1.7.0

What platform are you seeing the problem on?

All

Relevant log output

E [ERROR] Parameter: result (of type List<T>) should match type Future<List<int>>.
  package:blub/model/api/temp.dart:6:14
    ╷
  6 │ Future<bool> _shouldCacheGeneric<T>(List<T> result) async => result.isNotEmpty;
    │              ^^^^^^^^^^^^^^^^^^^
    ╵

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions