You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I will comment since you said you wanted to see a use case.
Partial delegation has a lot of uses cases, at least for someone coming from Java land. Java programmers pretty commonly write classes that overwrite just a single method or a few methods of a superclass or interface. In plain Java, you still have to explicitly write forwarding methods for all of the methods that you aren't overriding, which sucks. Kotlin fixes this with its delegation feature. I had assumed that "delegation" meant "full or partial delegation" when I tried to use this crate, but I had to back out because only full delegation is possible.
Here's the code where I wanted to use this pattern: https://github.com/garfieldnate/ray_tracer_challenge/blob/7efd79c569e814a4ee8a9620bf77369143df6da6/src/shape/shape.rs#L64. As I'm still learning Rust I might just be doing this in a non-idiomatic way, but I'll explain what I was trying to do anyway. All objects that implement Shape will have to keep track of some simple state fields. The trait itself cannot hold fields, so I created BaseShape to hold the fields and take care of some basic state maintenance. Now, other implementations of Shape will contain a BaseShape in a field and delegate everything except local_intersect and local_norm_at to this field.
Since I basically did copy-paste everywhere to make the Shape implementations delegate to a BaseShape instance, I accidentally added bugs pretty quickly. If I could instead automatically delegate all methods that are not already overridden to the BaseShape field, it would be much less error-prone.
See https://github.com/elahn/rfcs/blob/delegation2018/text/0000-delegation.md:
Still needs some adapted syntax for the macro variant in this crate.
Would also be interesting to have a good example use-case for this, as I can't come up with any good ones.
The text was updated successfully, but these errors were encountered: