From: Sean O'Dell Date: 2003-11-20T06:29:12+09:00 Subject: Re: "stereotyping" (was: Re: Strong Typing (Re: Managing metadata about attribute types) On Wednesday 19 November 2003 01:03 pm, Austin Ziegler wrote: > On Thu, 20 Nov 2003 03:52:30 +0900, Sean O'Dell wrote: > > On Wednesday 19 November 2003 10:27 am, David A. Black wrote: > >> A Ruby module or class is neither a contract, nor a fixed > >> collection. I really don't think, for example, that what Betrand > >> Meyer meant by "design by contract" was Ruby classes :-) > > > > A contract is any interface, and a Ruby class is an interface. > > True, a Ruby class isn't the definition of a contract, but any OO > > class serves the function well enough. Despite Ruby's flexibility, > > at any given point, a class implements an interface. It can be > > extended or changed, but it definitely describes an interface. > > A class defines a collection of methods that could be called an > interface. However, an instance of a Ruby class need not implement > that interface. Again, a pathological example: > > f = File.open("foo.txt") > class << f > undef write > end > > The object referenced by f *is* a File, but it doesn't support > #write. I agree. I don't advocate STRONG checking, just simple declarations that "this object implements this interface" and it does, it does, if it doesn't, it doesn't. > >> What you're doing is pressing the closest thing Ruby has to > >> something that *looks* like the kind of type-checking you want > >> (i.e., kind_of?) into service as a type check, which it isn't. > > > > Yes, but no one cares if an object being passed in is really of a > > certain type, all they care about is if it has the interface their > > method(s) need. I don't want a type check, I just want some > > enforcement that an interface exists where I need it. > > Which #kind_of? does *not* do. *Why* do you want or need that > enforcement? When I first used dynamic languages, I wanted them to > act like the static languages that I worked with, but I quickly grew > out of it. I briefly experimented with #kind_of? checking in Ruby, > but found few cases where I really needed it. That's good, because you have no choice. Doing kind_of? isn't terribly flexible. It requires inheritance, and that means people have to write abstract classes in order to allow people to re-implement classes. I don't advocate inheritance as a means to communication conformity with an interface. Just a keyword that says "I implement this interface" is perfectly acceptable. I would also wager that if it were available, and powerful, you would return to at least a little bit of type checking. People only miss something for so long before they get used to not having it. Of course you don't miss it now. Sean O'Dell