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: Abstract interface #9297

Closed
lachbaer opened this issue Feb 29, 2016 · 1 comment
Closed

Proposal: Abstract interface #9297

lachbaer opened this issue Feb 29, 2016 · 1 comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue

Comments

@lachbaer
Copy link
Contributor

During discussing #9132 I had the idea to propose abstract interface

public abstract interface IAnimal {
    void Walk();
    int Legs { get; set; }
}

When you use an abstract interface the compiler can check at compile time if the handed class instance implements all of the methods, events and properties of it. You can then pass any class that fits.

public class Cat {
    void Walk();
    int Legs { get; set; }
    void Jump();
}

public void Go(IAnimal animal) {
  animal.Walk();
}

public static void Main() {
  var myCat = new Cat(); 
  Go(myCat); // compiler checks if myCat fulfills the IAnimal abstract contract
}

In case of a cat that has paws instead of legs I think it would be better to stick with the current standard and define a derived class with an corresponding copy constructor

public class CatWithLegs : Cat {
  public CatWithLegs(Cat _originalCat) { /* copy */ }
  public int Legs {
    get => base.Paws; // 7881
    set => base.Paws = value;
  }
}

It should not be possible to 'derive' from an abstract interface, like with an abstract class definition, just to stay consistent.

@gafter
Copy link
Member

gafter commented Feb 29, 2016

I think proposing a concrete syntax for a different issue is best done inside that issue's discussion thread. Closing as a dup of #9132.

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

No branches or pull requests

3 participants