Mixins Without Multiple Inheritance

by Joe Strout





Listing one



Interface PropertyHolder

  Function PropCount() As Integer   

  Function PropName(index As Integer) As String   

  Function PropValue(index As Integer) As Variant

  Sub PropValue(index As Integer, assigns newValue As Variant)

End Interface





Listing Two



Function PropIndex(extends obj As PropertyHolder, name As String) As Integer

   Dim i As Integer

   for i = 0 to obj.PropCount - 1

      if obj.PropName(i) = name then return i

   next

   return -1

End Function



Function PropValue(extends obj As PropertyHolder, name As String) As Variant

   Dim idx As Integer = obj.PropIndex( name )

   if idx > 0 then

      return obj.PropValue( idx )

   else

      return nil

   end if

End Function



Sub PropValue(extends obj As PropertyHolder, name As 

                                      String, assigns newValue As Variant)

   Dim idx As Integer = obj.PropIndex( name )

   if idx > 0 then

      obj.PropValue( idx ) = newValue

   else

      raise new KeyNotFoundException

   end if

End Sub





Listing Three



Function PropIndex(extends obj As PropertyHolder, name As String) As Integer

   if obj IsA FastPropHolder then

      return FastPropHolder(obj).PropIndex( name )

   end if

   

   Dim i As Integer

   for i = 0 to obj.PropCount - 1

      if obj.PropName(i) = name then return i

   next

   

   return -1

End Function









2



