Has anyone built a compiler plugin that gives lamb...
# language-proposals
z
Has anyone built a compiler plugin that gives lambdas & anonymous functions more meaningful implementations of `equals`/`hashcode`/`toString`? I work with a lot of code that creates data classes to hold both data and functions (event handlers), and it would be great to actually define lambda equality in a meaningful way. Especially in tests, debugging output, etc. I’ve put some ideas into a twitter thread and as great an excuse as this would be to learn the IR plugin API, would love to just play with someone else’s if it exists.
e
React has
useCalllback()
which wraps a function so that equality is compared based on its dependencies (which also have to be explicitly given)
in Kotlin, I think something like this would still feel fairly natural,
Copy code
Model(callback = { foo }.withIdentity(foo))
IMO this is more useful than comparing based on bytecode or source location
z
It requires more boilerplate though, and in a huge existing codebase, that’s a non-starter
e
the compiler could probably automatically determine what the captured variables are instead of having to pass them by hand... but in a lot of cases the compiler captures
this
which is more scope than you want or need
z
the compiler could probably automatically determine what the captured variables are instead of having to pass them by hand
Isn’t that basically the same thing i was proposing then?
Or you’re saying it wouldn’t capture
this
?
e
the compiler will capture
this
if you're using a property, but writing it by hand you can "capture" only the property in use
z
ah, i see what you mean