From: Sean O'Dell Date: 2003-11-22T04:14:11+09:00 Subject: Re: "stereotyping" (was: Re: Strong Typing (Re: Managing metadata about attribute types) ) On Friday 21 November 2003 11:06 am, Weirich, James wrote: > > This is a wonderful idea. Let me restate it to make sure I > > understand it. Just as you can do the following: > > > > class Person > > def greet(other_person) > > other_person.to do > > say "hello" > > end > > end > > end > > > > class Person > > def some_other_method > > end > > end > > > > > > ...(I've defined class Person somewhere, but I can always open up the > > class again and define something else in it): it would be > > wonderful if you can *either* inline it *or* make it external in > > a similar fashion. [...] > > I think the ability to do this would make the whole idea a lot more > > palatable to a lot of us. > > I've been thinking about DbC stuff for some time and something in Matz Ruby > 2 talk last Saturday sparked an idea along these lines ... > > class Person > def greet(other_person) > marry(other_person) > end > end > > Then specify the contract in a separate file ... > > class Person > def greet:pre(other_person) > assert ! self.married? > assert ! other_person.married? > end > > def greet:post(other_person) > assert other_person.married? > assert self.married? > assert_equal self, other_person.spouse > assert_equal other_person, self.spouse > end > end > > An aside: Unfortunately, the post method doesn't have access to the result > returned from the function. In this case it doesn't matter, but many post > conditions assert something interesting about the result. I guess I would > have to use a wrap method for that. How would this work: def greet(other_person) if (shake_hand(other_person) == not_clammy) then marry(other_person) end end It seems that you could only pre-assert that objects going in had a certain range of values that fit the requirement. The above example wouldn't work because it depends on the result of a method call. Would it be logical to have some checks for a method done in one place, but other checks done somewhere else? Or would pre-assertions just be considered part of the method calling contract? it sort of feels like code logic is spread out, making the human brain work harder to bring it all back into one place conceptually. Sean O'Dell