How to implement call caching (Memoization)


I want to implement call cache(memoization) in non-intrusive way with the metadata annotations.

Hopefully, it will work like this:

class A{
  foo(msg) {
    return msg;

void main() {
  var foo = ()=>"hello";

Can it be achieved with only dart:mirrors ?


I wrote a whole blog post on this topic a while ago. It’s too long to copy here, so here’s the link:

The upshot is that you can write higher-order memoizing functions, but they’re limited in generality by Dart’s lack of flexible args functions. Also, if you want to use dynamic programming with recursive functions, you need to write your function with memoization in mind – it needs to take itself as an argument, so you can pass in the memoized version.

Answered By – Justin Fagnani

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *