From: Edgardo Hames Date: 2006-02-17T04:57:21+09:00 Subject: Re: OT: Is this worth a try? On 2/16/06, Logan Capaldo wrote: > > On Feb 16, 2006, at 12:35 PM, Edgardo Hames wrote: > > > On 2/16/06, Jens Auer wrote: > >> > >> Design by contract is a little bit more than placing asserts at the > >> beginning and end of a method. I would be realy glad if someone > >> points > >> me to some library wich implements DbC completely in Ruby, eg the > >> fact > >> that a method can only widen the supermethod's contracts which is > >> automatically checked. > > > > I assume that "automatically checked" means checking the contracts at > > compile time. > > That's an undecidible problem. > > > > No he means that a override of a method implemented in a subclass can > only widen the contract, and that that is automatically checked. Not > that the contract is fulfilled at compile time. > > e.g. > > class A > def meth(n) > precondition: n > 1 > end > end > > class B < A > def meth(n) > precondition: n > 0 #Ok, because any inputs that were valid > for A#meth are valid for B#meth, this is checked at compile time > end > end > > class C < A > def meth(n) > precondition: n > 2 #Will not compile, C#meth accepts a narrower > range of inputs than A, does not follow Liskov Substitution Principle > end > end > > Note this is not the same thing as checking whether a call will > fulfill conditions at compile time, just that the set of values > accepted by a subclasses overridden version of a method is a > superset of the set of values accepted by the parent's version of the > method. That's true only for preconditions. However, postconditions in subclasses should strengthen that of parent's, i.e. child's returned values are a subset of parent's. Cheers, Ed -- Encontr� a "Tu psic�pata favorito" http://tuxmaniac.blogspot.com Thou shalt study thy libraries and strive not to reinvent them without cause, that thy code may be short and readable and thy days pleasant and productive. -- Seventh commandment for C programmers