proposal-function-memo icon indicating copy to clipboard operation
proposal-function-memo copied to clipboard

Closures being handled like arguments?

Open dfahlander opened this issue 2 years ago • 1 comments

Maybe this would be another proposal, where different functions could be identified uniquely based on their source location, arguments and closures despite a new Function instance is created.

If so, closures could be handled like arguments in the sense that if the value of a closure is modified, a memoized function would have to re-evaluate?

This would be the real game changer IMHO as things like

import { useEffect } from "react";

function MyComponent({x}) {
  useEffect(
    ()=>doSomethingWith(x),
    [x]
  );
}

could simply be

import { useEffect } from "react";
function MyComponent({x}) {
  useEffect(
    ()=>doSomethingWith(x)
  );
}

Specifically in this react sample, that would minimize user errors in identifying closures in effects, but generally this could be valuable for other libraries that make use of memoized functions that depend on closures.

I understand the arguments to be immutable tuple or records, so I suppose the closure would need to be such (or else converted to a symbol as is described in the proposal).

dfahlander avatar May 30 '23 09:05 dfahlander

Since that would require being informed by syntax, it would need a solution that was also syntax - which means the current API a form couldn’t ever have this ability, and a syntax form would have a much harder road in the committee.

ljharb avatar May 30 '23 14:05 ljharb