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

PROPOSAL: Constraint Inheritance / Inference #8304

Closed
TonyValenti opened this issue Feb 2, 2016 · 3 comments
Closed

PROPOSAL: Constraint Inheritance / Inference #8304

TonyValenti opened this issue Feb 2, 2016 · 3 comments
Labels
Area-Language Design Discussion Resolution-Duplicate The described behavior is tracked in another issue

Comments

@TonyValenti
Copy link

Today in C#, constraints on methods get automatically "inherited" by derived classes and do not need to be redefined. This is a good thing and helps keep my code clean, concise, and readable, and when you need to refactor a base method, you only have to refactor the base method.

    public class BaseClass {
        public virtual string DoSomething<T>(T Item) where T : new() {
            return new T().ToString();
        }
    }


    public class DerivedClass : BaseClass {
        public override string DoSomething<T>(T Item) 
            //where T : new() <- This is not necessary.
            {
            return base.DoSomething<T>(Item);
        }
    }

That is all goodwhen you're working with generic methods, but when you're working with Generic classes, you have a different story. Here is an example:

    public class BaseClass<T> where T : new() {
        public virtual string DoSomething(T Item) {
            return new T().ToString();
        }

    }

    public class DerivedClass<T> : BaseClass<T>
        //This line should not be needed but it is.
        where T : new()
        {

    }

Today, with a class definition like this, if I refactor BaseClass<T> and add a new constraint onto <T>, I then have to go find every derived class that extend's BaseClass<T> and copy/paste that new constraint into DerivedClass<T>'s definition. This has a couple of problems:

  1. I have to clutter DerivedClass<T> with redefinitions of base class stuff.
  2. When refactoring the base class, I am forced to refactor the derived class.

In the example above, DerivedClass's <T> should automatically "inherit" the constraints that are placed on it by BaseClass<T>.

In order to simplify things and preserve compatibility with existing code, using the where clause in a derived class should specify additional constraints that may or may not already be inferred from its usage.

@TonyValenti TonyValenti changed the title Constraint Inheritance / Inference PROPOSAL: Constraint Inheritance / Inference Feb 2, 2016
@axel-habermaier
Copy link
Contributor

Eric Lippert outlined the problems with this proposal in an interesting-to-read (as usual) blog post.

@alrz
Copy link
Member

alrz commented Feb 2, 2016

Dupe of #8041

@gafter gafter added Resolution-Duplicate The described behavior is tracked in another issue Discussion labels Feb 8, 2016
@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Discussion Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

5 participants