[Proposal]: Implicit Interface with mandatory default implementations #8406
Unanswered
Caracrist
asked this question in
Language Ideas
Replies: 1 comment 12 replies
-
This sounds like a less useful version of extensions. #5497 |
Beta Was this translation helpful? Give feedback.
12 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: Implicit Interfaces in C#
This proposal introduces a new type of interface in C#, called an implicit interface.
In this model, all other interfaces would be considered “explicit” (with an optional keyword for backward compatibility). Implicit interfaces aim to enhance the language’s flexibility by allowing interfaces to group existing interfaces with mandatory default implementations of new functions and properties (a feature introduced in C# 8.0). This approach adds new ways to manage and utilize interfaces without requiring modifications to existing implementation classes.
Proposed Feature
Implicit Interface:
An implicit interface is a specialized type of interface characterized by the following:
Contents
An implicit interface can only contain methods and properties that are new additions, with mandatory default implementations provided in the interface itself.
Inheritance and Implementation
Implicit Conversion
is
andas
and genericwhere
type matching.New Operators
is explicit
operator: Returnstrue
only if the actual type explicitly implements the interface on the right value.as explicit
operator: Attempts to cast a type to the implicit interface, returning the cast only if the type can be converted to the implicit interface and explicitly implements it.where
(generic type constraint): explicit: Using the keywordexplicit
before an interface name in thewhere
clause should restrict the matching for types that implement that interface explicitly only.explicit cast
operator: Adding the keywordexplicit
before the interface name in the cast operator, like this:(explicit Interface)instance
, would restrict the cast operation to succeed only if the instance type implements that interface explicitly.is implicit
,as implicit
,(implicit Interface)
cast operators, andwhere : implicit
clause: These operators and clause should be added for the sake of symmetry and act identically to the existingis
,as
, and cast operators, and existingwhere
clause.Key Benefits
Non-Breaking Extensions
Implicit interfaces allow for the non-breaking introduction of new members to existing interfaces, especially those based on multiple base interfaces. This feature is highly beneficial in evolving APIs and libraries without requiring changes to existing implementations.
Examples:
Extending Existing Interfaces
Implicit interfaces enable extending existing interfaces with additional functionality without altering the existing implementations. This is particularly valuable in scenarios involving widely used interfaces like
ILogger
.Now, any class implementing
ILogger
can be implicitly treated as implementingIFatalLogging
, and theFatal
method can be called without modifying the existing classes:Grouping Interfaces
Implicit interfaces provide a way to group multiple interfaces and use this grouping to access the collective functionality. This feature offers a cleaner way to handle complex scenarios where a class needs to implement a collection of interfaces without cluttering the class design.
Example:
Suppose you have multiple interfaces related to file operations:
An implicit interface
IReadWrite
can group them:Any class that implements both
IReadable
andIWritable
will automatically be considered as implicitly implementingIReadWrite
. For example:This approach makes code management cleaner and more intuitive.
Grouping and Extending
Implicit interfaces can also group and extend existing interfaces simultaneously. For example, an implicit interface could combine
IFlyable
andISwimmable
while adding new functionality.Example:
Suppose we have:
Any class implementing
IFlyable
andISwimmable
can be implicitly treated asIAquaticBird
, gaining the newDive
functionality without modifying the existing classes. For example:Conclusion
The introduction of implicit interfaces in C# would significantly enhance the interface-based programming model by allowing non-breaking extensions and grouping of related interfaces. By adding new members with default implementations, implicit interfaces enable more dynamic and flexible ways to extend and manage interfaces, making C# an even more powerful and adaptable language for developers.
Beta Was this translation helpful? Give feedback.
All reactions