[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Duck Typing

Chad Fowler

9/14/2003 10:19:00 AM

1 Answer

Mauricio Fernández

9/14/2003 11:45:00 AM

0

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:
>
> <quote source="http://mephle.org/StrongTyping...
> 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