From: "Mauricio Fernández" Date: 2003-09-14T20:45:12+09:00 Subject: Re: Duck Typing On Sun, Sep 14, 2003 at 07:19:07PM +0900, Chad Fowler wrote: > On Sun, 14 Sep 2003, Ryan Pavlik wrote: > class String > undef_method :chomp > end > > def some_method(a,b) > expect(a, String, b, Numeric) # I'm using your "Strong"Typing module. ;) > a.chomp #Whoops! "a" is not the duck you're looking for > # (but it *is* a String)! > ...etc. > end > > Since I happen to be on the topic of your "Strong"Typing module, let's > have a look at the FAQ: > > > Q: Yeah, but really, why bother? Why not just let ruby sort out the > errors as they occur? > A: This is incorrect thinking. Allowing errors to just occur when > they happen is naive programming. Consider the following: > > # Wait N seconds, then open the bridge for M seconds > def sendMsg(bridge, n, m) > sleep(n) > bridge.open > sleep(m) > bridge.close > end StrongTyping doesn't address that problem, careful programming does: def sendMsg(bridge, n, m) open = false begin sleep n bridge.open # either completes correctly or raises an exception before opening open = true sleep m bridge.close ensure bridge.close if open end end As Chad points out, even StrongTyping assumes that the interface of a class will remain stable. This means that as long as methods can be removed/modified, the notion of "type" enforced by StrongTyping doesn't match Ruby's native one, so you cannot get 100% safety. -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com 'Ooohh.. "FreeBSD is faster over loopback, when compared to Linux over the wire". Film at 11.' -- Linus Torvalds