From: Eivind Eklund Date: 2007-12-07T19:55:03+09:00 Subject: Re: Worth an RCR? static_type_check, polymorphic_type_check,quacks_like On Dec 6, 2007 11:32 PM, John Carter wrote: > Yes, yes, yes, I know all the theory, I've read the arguments....and > I'm not suggesting that one make every method call have strictly type > parameter checking. > > I don't. I'd say I don't do parameter type checking in 98% of my > methods. > > But believe you me, when I hit a bug where I invoke a method that dies > because there is something screwy in an instance variable and the bug > isn't on the backtrace..... > > I say, stuff the theory, slap one of those beauties in the constructor > and find the bug instantly. I wrote code to do this 4 years ago. It is a framework that does it by writing a "typesig" declaration before your method declaration (or through various other methods of applying them). The type checks allow a LOT of flexible type declarations, including respond_to checks, class/module checks, conditional checks depending on previous type matches, repeated sequence matches against varargs, etc, etc, etc. Matching your example, you would declare class MyObject typesig :exact, String, Drawable, Type::Respond(:border_color, :fill_colour) def initialize( _name, _drawable, _theme) @name = _name @drawable = _drawable @theme = _theme end end Overall, I don't feel that it gave me that much to use it anyway - more trouble than it was worth - but in my opinion, it solves the problem better and if we are going to provide more type checking support, that's a better place to start. The form of declaration keep the type checks normalized and allow us to turn them off for production if there is a speed penalty (which makes it possible to have expensive type checks in there), and it keeps the declaration at the same point as the parameter list, decreasing noise in the code itself. The code is available here: http://people.freebsd.org/~eivind/ruby/types/ I am willing to do more work on it if anybody actually use it, and I am willing to let somebody else take over maintenance if they prefer. BTW: If we are going to discuss adding more type checking, I'll note that the place where I feel it could primarily be of value is at the "edge" of library code, where it would function as more formal documentation of the calling convention than th present variants, with the added bonus that you know it is sort of correct. Eivind.