Comment by KolmogorovComp

14 days ago

This is correct. But it’s not really surprising, it’s type inference.

It isn't really type inference. Each closure gets a unique type. Rather it's an automatic decision of what traits (think roughly "superclasses" I guess if you aren't familiar with traits/typeclasses) to implement for that type.

  • So you're saying... it's type inference of type classes, just like in Haskell?

    • No, I don't think so, not unless there's some feature of Haskell type classes I'm completely unaware of.

      If anything it's closer to SFINAE in C++ where it tries to implement methods but then doesn't consider it an error if it fails. Then infers type-classes based on the outcome of the SFINAE process. Or the macro analogy another poster made isn't bad (with the caveat that it's a type system aware macro - which at least in rust is strange).

    • I am not sure how Haskell works but I think what the previous poster meant is that the types get determined at compile time. Closures are akin to macros except you can't see the expanded code.