4 ms·
One of the pieces of OO _inheritance_ I deeply miss in Rust is the ability to subclass a library class, override *one* function, and retain all the other functi
by ldiracdelta 5y ago
One of the pieces of OO _inheritance_ I deeply miss in Rust is the ability to subclass a library class, override *one* function, and retain all the other functionality. Yesterday, I needed to tweak the behavior of one function on actix-identity's cookie and my option seemed to be to wrapper the original object and write the same interface for the entire object, passing through every function except the one I wanted to override. Am I a sinner for wanting inheritance yesterday? Probably, but I had to write so much boilerplate to achieve my tiny override.
- pjmlp 5y agoI guess one way would be via delegation, similar to how COM does it. I bet with a couple of macros it would be possible to automate what languages on Windows can somehow do magically with COM interfaces.
- estebank 5y agoYou don't need any macros, it can be accomplished with the trait system[1], either manually or through auto-deref. Some of the boilerplate could be generalized with a derive proc-macro or a macro-by-example call but it is not absolutely necessary. [1]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8829ae8be84b9122aca5173f318505cb https://play.rust-lang.org/?version=stable&mode=debug&editio...
- ldiracdelta 5y agoThank you so much for this.
- kinjba11 5y agoYou don't necessarily need inheritance. Kotlin for example has a feature wherein all that forwarding boilerplate you had to write is automated. https://kotlinlang.org/docs/delegation.html https://kotlinlang.org/docs/delegation.html
- ldiracdelta 5y agoThat sounds fantastic. It also sounds a bit like a vtable :-D