From: Chris Gehlker Date: 2002-10-17T00:45:41+09:00 Subject: Re: Interfaces in Ruby On Wednesday, October 16, 2002, at 07:12 AM, Paul Brannan wrote: > On Wed, Oct 16, 2002 at 01:30:04PM +0900, Chris Gehlker wrote: >> >> On Tuesday, October 15, 2002, at 09:02 PM, Edward Wilson wrote: >> >>> Is there a way to write/inforce interfaces in Ruby like one can using >>> Abstract Classes in C++ or Interfaces in Java? >>> >>> If not, how has everyone been approaching this? >>> >> I don't think C++ abstract classes are that similar to Java >> interfaces. > > Not having used Java interfaces extensively, I can't speak about their > subtleties, but Java interfaces and C++ abstract classes are used for > similar functions. Both are used to say "this class implements this > set > of functions." This is particularly important in C++, since, through > the magic of virtual functions, it allows me to write a single function > that can accept objects of many different types, without using > templates. This is less useful in Ruby, since an object always retains > its type when I pass it as a parameter to a method (whereas in C++ I > only have a reference to the base class). > >> I don't see the need for interfaces in a language with mix-ins. > > This doesn't make sense to me. I see interfaces and mixins as > orthogonal concepts; interfaces are used to specify a contract, while > mixins are used to provide additional methods to a class. > It's just that the contract is used as a weak form of multiple inheritance. At least I've never seen it used any other way. And it is called "Interface Inheritance" because it allows the programmer to add functionality to a class that is orthogonal to the functionality of the main inheritance path. For example if bird inherits from animal and implements flight then a bird 'is a' animal but a bird also 'is a' flier. In Ruby I'd just make flight a mixin. >> When I want something like a C++ abstract class, I use a class >> cluster. Maybe that's just my ObjC background showing. > > What is a "class cluster"? Can you provide an example? Lets start with the hoary example from C++ of the abstract class shape whose only non-virtual methods are move and resize where resize operates on a circumscribed rectangle and whose only attributes are the coordinates of the bounding rectangle. To do something similar in dynamic languages, you define a class shape with move and resize methods but you make shape's new method private. Then you define concrete constructors such as create_circle_with_radius(r) and create_rectangle_with_sides(length, width) etc. Of course you derive circle, rectangle, pentagon and friends from shape. You can call draw(boundingBox) in shapes move and resize methods so long as every child of shape defines a draw method. Shape is called a class cluster because the private constructor guarantees that any variable referencing a shape is actually referencing a member of a concrete subclass. Use it as you would an abstract class. I hope that helps. It was pretty condensed so ask again if it's confusing. I'll try to provide a better example. -- Cogito Ergo Spud. - I think therefore I yam.