What about relaxed interface implementation ?
Posted
Fri, Apr 7 2006 23:54
by
bill
VB9 will include relaxed delegates which will allow you to do things like handle more specific events in a general event handler. By the same principles though we could also have relaxed interface implementation. For example, consider the following code:
Interface IFoo
Function GetThing() as Object
Sub DoStuff(thing As String)
End Interface
Public Class Test
Implements IFoo
Function GetThing() As String Implements Ifoo.GetThing
return ""
End Function
Sub DoStuff(thing As Object) Implements IFoo.DoStuff
End sub
End Class
There the GetThing member is implemented as returning a more derived type, hence always safe. For parameters of course it's the other way around, as shown with the DoStuff implementation.
So why would you want this ? Well consider many of today's Generic Interfaces that often have a partner pair with a non generic interface. Today you need to implement both, and it can quickly resemble spaghetti ;) Whereas if you had relaxed interface implementation, you could generally use the one method for a member of both he generic and non generic interface.
Code such as :
Class foo
Implements IEnumerable(OfString)
PublicFunction GetEnumerator() As IEnumerator(OfString) Implements IEnumerable(OfString).GetEnumerator
EndFunction
PublicFunction GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
EndFunction
EndClass
would become:
Class foo
Implements IEnumerable(OfString)
PublicFunction GetEnumerator() As IEnumerator(OfString) Implements IEnumerable(OfString).GetEnumerator, IEnumerable.GetEnumerator
EndFunction
EndClass
code like:
Class foo
Implements IEnumerator(OfString)
PublicReadOnlyProperty Current() AsStringImplements IEnumerator(OfString).Current
Get
EndGet
EndProperty
PublicReadOnlyProperty Current1() AsObjectImplements IEnumerator.Current
Get
EndGet
EndProperty
….
End Class
would become:
Class foo
Implements IEnumerator(OfString)
PublicReadOnlyProperty Current() AsStringImplements IEnumerator(OfString).Current, IEnumerator.Current
Get
EndGet
EndProperty
….
End Class