On Jun 24, 2013, at 06:29 , Marvin Humphrey <[email protected]> wrote:

> I see you've now opened an issue about interfaces.
> 
>    https://issues.apache.org/jira/browse/LUCY-258
> 
> I agree that those would be a very useful feature.  For instance, it would
> allow us to clean up the mess I made of Freezer.c once we removed Serialize()
> and Deserialize() from Clownfish::Obj.
> 
> One limitation of "Java-style interfaces" is that all interface methods must
> be abstract.  I've never understood the rationale for that.
> 
> Is there any reason that we couldn't allow default implementations?  Consider
> the Ruby mixin Comparable:
> 
>    http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html#S2
> 
>    The Comparable mixin can be used to add the comparison operators (<, <=,
>    ==, >=, and >), as well as the method between?, to a class. For this to
>    work, Comparable assumes that any class that uses it defines the operator
>    <=>. So, as a class writer, you define the one method, <=>, include
>    Comparable, and get six comparison functions for free.
> 
> Comparable provides one abstract method and six default implementations
> defined in terms of that abstract method.  Subclasses which implement
> Comparable are welcome to override the default implementations for the sake of
> efficiency, but often that won't be necessary.
> 
> Ruby is a little more flexible in that mixins have access to instance
> variables, whereas our interface/mixin methods would not -- they would only be
> allowed to access other methods defined in the same interface.  But it still
> seems worthwhile to allow non-abstract methods.

Makes total sense. The default implementations would simply work on 
Clownfish::Obj objects.

> For Perl at least, I imagine we'd use multiple inheritance.
> 
>    package MyObj;
>    use base qw( Clownfish::Obj Clownfish::Comparable );

Yes, for dynamically typed languages which lookup methods by name, we don't 
even need an interface type with extra bindings for the interface methods. For 
Perl, an interface could be an empty class that's only used for ISA checks. The 
interface methods would still be correctly dispatched via the implementing 
class.

Nick

Reply via email to