Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for partial delegation #13

Open
hobofan opened this issue Dec 23, 2019 · 1 comment
Open

Add support for partial delegation #13

hobofan opened this issue Dec 23, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@hobofan
Copy link
Owner

hobofan commented Dec 23, 2019

See https://github.com/elahn/rfcs/blob/delegation2018/text/0000-delegation.md:

impl TR for S {
    delegate * to self.f;

    fn foo(&self) -> u32 {
        42
    }
}

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.

@hobofan hobofan changed the title Add support for partial derives Add support for partial delegation Dec 23, 2019
@hobofan hobofan added the enhancement New feature or request label Jan 20, 2020
@garfieldnate
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants