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
 
 
 
Filed under: ,

Comments

# re: What about relaxed interface implementation ?

Saturday, April 08, 2006 10:46 PM by Jonathan Allen

That would be far more useful to me then the implied interfaces they are working on. (Not that I think implied interfaces are useless, I just don't expect to use them very often.)

# re: What about relaxed interface implementation ?

Sunday, April 09, 2006 7:43 PM by Erik Meijer

Great idea; we should be able to use same kind of stubs as we do for events.

Erik

# re: What about relaxed interface implementation ?

Sunday, April 09, 2006 8:34 PM by bill

Thanks Erik :)

# Implied Interfaces

Tuesday, April 08, 2008 12:34 AM by @ Head

One of the many things I love about VB is it's explicit interface mapping . It's the beauty of

# Implied Interfaces - @ Head

Tuesday, April 08, 2008 12:34 AM by Implied Interfaces - @ Head

Pingback from  Implied Interfaces - @ Head