From: "Ara.T.Howard" Date: 2003-11-22T01:27:19+09:00 Subject: Re: "stereotyping" (was: Re: Strong Typing (Re: Managing metadata about attribute types) ) On Sat, 22 Nov 2003, Mark Wilson wrote: > My own views on interface crystallization: (similar to my own) > 1. It should not be done in-line, but rather through a separate express > creation of an interface table. [Note: one of the nice things about the way > Ruby (and other languages) does things is the inline creation of dispatch on > type tables for generic operators through the use of classes in an object > system. It would be bad to complicate this and other elements of Ruby's > human interface by also putting interface "contracts" inline with Ruby's > current (usually very comprehensible) class declaration system.] i thinks it's absolutely in line with the ruby way for an 'interface' to simply be an object class Interface def initialize object = nil skeletonize object if object end end > 2. The table would include fields specifying whether the interface is > mutable or immutable, the number of arguments and "types" of arguments a > method takes, exceptions to raise if the particular interface element fails, > and so on. this too, should simply be an object class Interface attr :signatures class Signature def initialize object = nil, meth = nil skeletonize object, meth if object and meth @signatures = Hash.new # map meth name to signature end end end > 3. A skeleton table could be generated from existing code, with suitable > default values (such as mutable interface, argument "type" set to Object, > etc. The developer would choose where to crystallize an interface element > and forego additional possible dynamism. skeleton_sig = Signature.new Array, :each class Interface class Signature # true/false def takes_block; end # attributes of block def block_attr; end # list of errors/exceptions types def raises; end # list of Classes, Interfaces, or methods any argument must respond_to? # example: sig.signature # => [String, StringInterface, :to_s] def signature; end end end > 4. The table could be queried to produce detailed documentation information > and/or included at runtime as a constraint on execution. see above > 5. In the future, one could perhaps also use the table to guide a Ruby > compiler as to when a form of "static typing" can be used to optimize a > particular program (according to the instructions of the developer). 6. Interfaces __HAVE__ implementations vs. classes __IMPLEMENTING__ an Interface. this is vastly simpler with respect to backward compat: BAD: class MyClass # requires change to class 'Class' implements Interface end GOOD: interface.impl << MyClass # no change to class 'Class' needed > I do not, however, think the proposal is necessarily a good idea. It > does seem very costly, and the magnitude of the benefit is not clear to > me. A way to make it lower-cost would be good, because then it would be > easier to try. again, agreed. but if people HAVE to have such a beast i concur that it should be a completely separate one - not tightly intergrated into the language. > Some questions: > How can Ruby better document the kind of block code that would be > useful with respect to a particular method? perhaps class Interface class Signature; end class MethodSignature < Signature; end class BlockSignature < Signature; end end ??? -a -- ATTN: please update your address books with address below! =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | STP :: http://www.ngdc.noaa.gov/stp/ | NGDC :: http://www.ngdc.noaa.gov/ | NESDIS :: http://www.nesdis.noaa.gov/ | NOAA :: http://www.noaa.gov/ | US DOC :: http://www.commerce.gov/ | | The difference between art and science is that science is what we | understand well enough to explain to a computer. | Art is everything else. | -- Donald Knuth, "Discover" | | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done' ===============================================================================