Comment by skrebbel

Comment by skrebbel 6 months ago

3 replies

This seems very similar to Java's oldschool single-interface callback mechanism. Originally, Java didn't have lambdas or closures or anything of the sort, so instead they'd litter the standard library with single-method interfaces with names like ActionListener, MouseListener, ListItemSelectedListener, etc. You'd make a class that implements that interface, manually adding whatever data you need in the callback (just like here), and implement the callback method itself of course.

I think that has the same benefit as this, that the callbacks are all very clearly named and therefore easy to pick out of a stack trace.

(In fact, it seems like a missed opportunity that modern Java lambdas, which are simply syntactical sugar around the same single-method interface, do not seem to use the interface name in the autogenerated class)

spullara 6 months ago

They don't autogenerate classes anymore, just private static methods though I agree that it would be nice to have more of the metadata in the name of the generated method.

  • skrebbel 6 months ago

    Oh really? Cool, I did not know that.

    How does that work with variables in the closure then? I could see that work with the autogenerated class: Just make a class field for every variable referenced inside the lambda function body, and assign those in constructor. Pretty similar to this here article. But it's not immediately obvious to me how private static methods can be used to do the same, except for callbacks that do not form a closure (eg filter predicates and sort compare functions and the likes that only use the function parameters).

    • spullara 6 months ago

      Ah there is some nuance. For capturing lambdas they do generate a class at runtime to capture the variables but it still then just calls the generated private method with the simplistic naming scheme. Also, apparently the simple naming scheme was chosen so as to not go down the C++ mangled name path and just depend on the debugging information.