From: Florian Gross Date: 2004-06-09T02:28:38+09:00 Subject: Re: Interfaces and semantics (or, how to hashpipe a duck) Hal Fulton wrote: > I think when Rich says "semantics" he means something that can't > really be checked by a machine -- the "purpose or function," as it > were. > I do sometimes wish there were a way to detect semantics -- but the > best we could do is simply remove the problem a step further. > For example, imagine that we are concerned with a conceptual "append" > operation. We want to know whether an object is capable of being > appended onto. > > Well, a string is "appendable." So is an array. So is a file. > I have at times (almost) wished for a thing rather like Java interfaces: > > obj.implements?(Append) # where Append is some sort of interface > # whose conceptual semantics are agreed on I think I'm going to share my ideas about all this. Oh, and sorry for the heavy quoting, but it's hard to snip well-written text. :) However this also applies to me right now: >> Warning: I'm tired, so I'm not expressing myself as well as I might >> at a different time of day. I would really like to do it like this: EachContract = Contract.new do def test(obj) assert(obj.respond_to?(:each)) end end AppendContract = Contract.new do implicates EachContract def test_append(obj) assert(obj.respond_to?(:<<)) initial = []; obj.each { |item| initial << item } new_obj = obj.clone; new_obj << "x" appended = []; obj.each { |item| appended << item } assert(appended.size > initial.size) assert(initial.all? { |item| appended.include?(item) }) end end obj = [] case obj when AppendContract then obj << 5 end This still needs to be thought to the end, however. Regards, Florian Gross