-
Notifications
You must be signed in to change notification settings - Fork 4k
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: default interface method implementation #16074
Comments
I don't see any benefit. If you like to provide a default, then why don't you provide an abstract class that implements the interface and that provides some partial implementation? In VB you can ship it together, the abstract class can be an inner class of the interface:
Whether this is best practice or not I let up to you to decide. But it's quite elegant to be consumed:
|
@HugoRoss From a C# perspective, extension methods are fantastic tools. This will not change. They are much more broadly applicable, if less conceptually pure, than default interface methods because they can augment any type and can do it transiently, i.e. when brought into scope by referencing an assembly and/or importing a namespace. However, the use case for default methods is to allow an imbued type to take part in the method selection process by providing its own implementation. They serve different purposes. |
Hi , @HugoRoss, In practice, using abstract class for the default method simulation just partly works: The disadvantage of abstract class simulate interface default method1. abstract class can not multiple inheritanceThe class can not multiple inheritance but the interface does, so if a class type implement 2 different interface and these 2 interface both have the default method implements, then the abstract class simulation failure to work 2. interface have function alias name, but abstract class it doesn'tFor example, Interface I
Default Function a As String
Return "123"
End Function
End Interface
Class a : Implements I
Private Overloads Function bb As String Implements I.a
Return "123"
End Function
End Class
Class b : Implements I
Public Overloads Function a As String Implements I.a
Return "233"
End Function
End Class
Class c: Implements I
End Class These function alias all works, Call DirectCast(New a, I).a.Echo ' 123
Call DirectCast(New b, I).a.Echo ' 233
Call DirectCast(New c, I).a.Echo ' 123 3. interface have function access control, but abstract class it doesn'tThe function overrides in class type Inheritance its signature must keeps the same, but the interface method implementation doesn't MustInherits Class cc : Implements II
' All of the interface method is Public access, but we can change its implements function access
' But this situation is not allowed in the class inherits
Private Function a As String Implements II.Function_b
End Function
Protected MustOverrides Function b As String Implements II.a123
Public MustOverrides Function c As String Implements II.bbb
End Class All of this access controls from the interface method are works perfectly in VisualBasic 3. abstract class can apply on Structure Type?NO! The disadvantage of extension method simulate interface default methodAssume that we have a extension method using as the default method implements for an interface type in namespace a, and another extension method for implements as a default method for a specific interface implements type in namespace b Namespace a
Public Module IDefault
' This method works for all type that implements interface I
<Extension> Public Sub DefaultExample(Of T As I)(x As T)
Namespace b
Public Module IDefault
' This method works for all type c2, and c2 Implements the interface I
<Extension> Public Sub DefaultExample(Of T As c2)(x As T) For example, if we want using the default method in |
@amethyst-asuka yes, this is the point! |
@amethyst-asuka Indeed. I fully agree. However for extension methods
this is actually a great thing. |
Closing as a dup of #258 |
Hi , @amethyst-asuka, Reply to "Abstract classes cannnot have multiple inheritance, aliases, change visibility and do not work with structures" You are right, abstract classes are not that convenient (although they work, of course). Here an example with concrete classes (my new suggestion for handling these cases without adding new syntax to the language).
This example shows a structure that uses default implementations of two different interfaces and changes the visibility of their methods. (And alright, maybe a slight addition to VB syntax would simplify it...) |
Hi, currently the interface definition in .NET just allow define a set of method declaration template, and not allow includes any implentation details. But the java language it does, the interface define in java can includes some default method implentation, and I thinks this feature can introduce in the .NET language too.
Actually, the interface with default method implentation in java language can be simulated in .NET language by using extension method of the interface type or abstract class
For example, using the extension method for this simulation:
Or using an abstract class is also works, but the problem is that the abstract class just not allow multiple inherits and the extension method is not so Convenient. So i think this java language feature can be introduce into VB/C#.
This default interface method is useful for some common operations of an interface type, and can save a lot of time from the duplicate coding for this job.
For example, in visualbasic define for the default interface method implementation in Interface with VB
Default
keyword:Were in this interface its implentation type, can overrides the default method with
Overloads
keyword:The text was updated successfully, but these errors were encountered: