Skip to content

Specialization should allow for delegation to default trait method implementation #68309

Open

Description

@jazzfool

Given the following trait:

trait Trait {
    fn test(&self) { println!("default implementation"); }
}

To provide a default specialization on Trait::test, test must be redefined;

impl<T> Trait for T {
    default fn test(&self) { println!("default implementation"); }
}

However this violates the DRY principle. The solution here would be to provide a way to delegate the implementation of test to the already existing default implementation found in the trait definition.

Proposed syntax to allow for this delegation:

impl<T> Trait for T {
    default fn test(&self);
}

Activity

  1. jazzfool commented on Jan 17, 2020

    @jazzfool
    Author
  2. added
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    T-langRelevant to the language team
    on Jan 17, 2020
  3. Centril commented on Jan 17, 2020

    @Centril
    Contributor

    I believe this is intentional fallout from #64564 (cc @jonas-schievink).

    Having some sort of forwarding syntax to recover DRY seems reasonable and default fn test(&self); isn't a bad one.

  4. jonas-schievink commented on Jan 17, 2020

    @jonas-schievink
    Contributor

    Yup, sounds reasonable to me. I noticed the amount of repetition when working on that PR too:

    One annoyance with doing this is that it sometimes requires users to copy-paste a provided trait method into an impl just to mark it default (ie. there is no syntax to forward this impl method to the provided trait method).

  5. Centril commented on Jan 17, 2020

    @Centril
    Contributor

    Syntactic support for default fn test(&self); in an impl should already be there after #67131. To support it semantically we would need changes in lowering and beyond.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-specializationArea: Trait impl specializationC-feature-requestCategory: A feature request, i.e: not implemented / a PR.F-associated_type_defaults`#![feature(associated_type_defaults)]`F-specialization`#![feature(specialization)]`T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions