[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

dblack

9/13/2003 1:30:00 AM

13 Answers

Jim Weirich

9/13/2003 2:43:00 AM

0

On Fri, 2003-09-12 at 21:29, dblack@superlink.net wrote:
> Or even, more generally, left with the impression that there''s an
> on/off switch: if you say "is_a?" a lot, then you''re not doing duck
> typing, whereas if you say "respond_to?" a lot, then you are -- when
> in fact it''s really Ruby that''s doing the duck typing, and you''re
> just deciding how to react to it.

Exactly!

> I think I remember the phrase "duck-typed language" from one of your
> posts a while ago, which I thought really summed it up (though it may
> be a harder phrase to break people in with :-) It captures the idea
> that the inferential, dynamic concept of type resides in the language
> itself.

Hmmm ... this (http://blade.nagaokaut.ac.jp/ruby/ruby-...) is the
only post from goggle that had my name and duck typed language. I''m not
sure its the one you were thinking about.

--
-- Jim Weirich jweirich@one.net http://onest...
-----------------------------------------------------------------
http://www.rub... -- Making is safe for Duck Typers everywhere!

Meino Christian Cramer

9/13/2003 8:09:00 AM

0

Hal E. Fulton

9/13/2003 8:13:00 AM

0

Meino Christian Cramer wrote:
> From: dblack@superlink.net
> Subject: Re: Duck Typing
> Date: Sat, 13 Sep 2003 10:29:52 +0900
>
> Hi, first of all: I am very happy to have a programming language with
> such a communicative and friendly mailinglist!!! :)
>
> GREAT!
>
> (Whether it will be a good idea or not to mirror the posts into a newsgroup
> -- we will see. Previously I had made bad experiences, since my email
> postbox was overloaded with spam. Spammers do scan newsgroups for
> addresses... but this is quite another topic...)
>
> Back to the ducks !
>
>
>>Or even, more generally, left with the impression that there''s an
>>on/off switch: if you say "is_a?" a lot, then you''re not doing duck
>>typing, whereas if you say "respond_to?" a lot, then you are -- when
>>in fact it''s really Ruby that''s doing the duck typing, and you''re
>>just deciding how to react to it.
>
>
> If both works: is_a? and respond_to? : What is the advance of one
> over the other beside the fact, that one has a cool name : "Duck
> typing"?
>
> Is one slower, less clean or ... ? I am just curious...

Don''t misunderstand. David is saying that this is *some* people''s
impression of what duck typing means.

Note the second part: "--when in fact it''s really Ruby that''s doing
the duck typing, and you''re just deciding how to react to it."

It''s not a coding style or technique; it''s an attribute of the
language.

Hal


Dan Doel

9/13/2003 9:57:00 AM

0

Meino Christian Cramer wrote:

> If both works: is_a? and respond_to? : What is the advance of one
> over the other beside the fact, that one has a cool name : "Duck
> typing"?
>
> Is one slower, less clean or ... ? I am just curious...
>
>

You really shouldn''t use #is_a? or #respond_to? much at all. That''s not what
duck typing is about.

Just write your method to use methods that the passed in object should
have, by
the contract of the method. Then test to make sure your program works.

Excessively using #is_a? and #respond_to? is essentially trying to turn ruby
from a dynamically typed language into an ad-hoc statically typed language,
which isn''t in the spirit of duck typing at all.

The only real reason you should be using these methods is if you need to
take different actions depending on which of several methods is available.
If you''re just checking whether the only method you use exists or not,
you''re
just doing extra work most of the time. The interpreter will tell you if it
isn''t there.

It can be a confusing issue if you haven''t read the discussions before.
There
was a big one some weeks back, so it might be worth it to read the ruby-talk
archives to clear some stuff up without duplicating all of it again.

Cheers.

- Dan


Meino Christian Cramer

9/13/2003 10:53:00 AM

0

Meino Christian Cramer

9/13/2003 10:57:00 AM

0

Martin DeMello

9/13/2003 11:53:00 AM

0

Dan Doel <djd15@po.cwru.edu> wrote:
>
> The only real reason you should be using these methods is if you need to
> take different actions depending on which of several methods is available.

Even then, there are cases where you can use the (cleaner or not
depending on your pov and how many times you have to do it) alternative
of creating a common name for the methods, and aliasing it to the
appropriate method within each class. In java terms, you''re using open
classes to retroactively derive from a common interface.

martin

Mauricio Fernández

9/13/2003 12:12:00 PM

0

On Sat, Sep 13, 2003 at 07:53:18PM +0900, Meino Christian Cramer wrote:
> Another code snippet I did in irb gaves me headaches also. Here it
> is:
>
> >> a=Array.new // a shiny new array, full of ducks ;)
> => []
> >> a=[[1,2],3] // fill in some extra ducks
> => [[1, 2], 3]
> >> a.respond_to?(:[]) // check whether we have an array
> => true // expected...
> >> a[0].respond_to?(:[]) // check wether first entry itself is
> // an array
> => true // ok, expected, too
> >> a[1].respond_to?(:[]) // now the "pure " 3...is it an array, too ?
> => true // OH! SURPRISE! It is an array...! (?)
> >> b=a[1] // lets put it in a clean variable
> => 3
> >> b.respond_to?(:[]) // now...
> => true // Again ... it seems to array-able

What you see is Fixnum#[]

-------------------------------------------------------------- Fixnum#[]
fix[ n ] -> 0, 1
------------------------------------------------------------------------
Bit Reference---Returns the nth bit in the binary representation of
fix, where fix[0] is the least significant bit.
a = 0b11001100101010
30.downto(0) do |n| print a[n] end
produces:
0000000000000000011001100101010

> What do I wrong here???
>
> Background is the following:
>
> I have either an hash of the following pattern
>
> a=Hash.new
>
> a=["<hexnum>", ["<id>","filename]]

I guess this is only your notation and not the code you''re feeding to ruby :)

> OR of that pattern:
>
> a=["<hexnum>", [["<id>","filename],["<id>","filename],....]]
>
> To print both versions, I have handle the value of the hash differently.
>
> Currently my method tries to figure out, how to print, with the
> help of "respond_to?".
>
> And it fails due to the above things I did in irb.

That''s because String has an instance method named [], too.
You''d have to use is_a? Array, but I''d rather change the code so that
in the first pattern you have an array of length 1.

ie

bla["0xff"] = [["bla", "file.rb"]]
bla["0x01"] = [["foo", "file2.rb], ["bar", "file3.rb"]]

This way the same code can work for both cases.

--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| ''_ \ / _` | __/ __| ''_ ` _ \ / _` | ''_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

...and scantily clad females, of course. Who cares if it''s below zero
outside.
-- Linus Torvalds

Ryan Pavlik

9/13/2003 7:22:00 PM

0

On Sat, 13 Sep 2003 17:13:17 +0900
Hal Fulton <hal9000@hypermetrics.com> wrote:

> > Is one slower, less clean or ... ? I am just curious...
>
> Don''t misunderstand. David is saying that this is *some* people''s
> impression of what duck typing means.
>
> Note the second part: "--when in fact it''s really Ruby that''s doing
> the duck typing, and you''re just deciding how to react to it."
>
> It''s not a coding style or technique; it''s an attribute of the
> language.

I disagree, with some proof by counterexample.

The assertion is this: Ruby does not have an attribute of the language
that is "duck typing", it is merely a style of coding.

1) By the definitions given in this thread, duck typing is taking a
given object and pretending it''s the type you want, and letting
the language give you an error if it doesn''t work like you
expected.

(If you disagree with this definition, you can provide an
alternate one, but arguing over definitions is useless.)

2) We can accomplish this because the language lets us specify a
call to a method without checking the type at "compile-time"
(parse-time, in ruby''s case):

x.a # This call is not validated until it is executed

3) We can further accomplish this because the language does not
require we specify types to a methodcall, thus allowing us to
pass any object.

4) Statements 2-3 are the only functional provisions necessary for
accomplishing the practice in statement 1.

5) Many non-Ruby dynamic languages provide the functionality in
statements 2-3. Examples include JavaScript, Common Lisp (CLOS),
Scheme, Python, Perl, and PHP.

6) These languages make no special provisions for duck typing, but
it can be practiced, because the functional requirements of 2-3
are met.

7) In any of the given languages, and Ruby, one can choose to check
types or not check types.

Therefore: Based on statements 1-5, "duck typing" is not specific to
ruby, but rather made possible by the dynamic nature of a language.
Based on statements 6-7, duck typing is neither specifically provided
for nor compulsory, so it is not an attribute of the language.

Thus, because it can be practiced, or not practiced; in ruby, or in
other languages; it must be a _style_ available in dynamic languages,
rather than a specific feature of a particular language.

--
Ryan Pavlik <rpav@mephle.com>

"Damn ye and ye black ops mind games!" - 8BT

Martin DeMello

9/14/2003 6:11:00 AM

0

Ryan Pavlik <rpav@mephle.com> wrote:

> Therefore: Based on statements 1-5, "duck typing" is not specific to
> ruby, but rather made possible by the dynamic nature of a language.
> Based on statements 6-7, duck typing is neither specifically provided
> for nor compulsory, so it is not an attribute of the language.
>
> Thus, because it can be practiced, or not practiced; in ruby, or in
> other languages; it must be a _style_ available in dynamic languages,
> rather than a specific feature of a particular language.

No, it''s related to the question "what is an object''s type?" as distinct
from "what is the class used to instantiate the object?". The phrase
"duck typing" is no more and no less than a shorthand for "an object''s
type is determined entirely by the set of messages it responds to", by
analogy with "static typing" (an object''s type is determined by a
compile-time declaration) and arguably as nothing more than a more
precise way of saying "dynamic typing".

Any yes, it''s by no means specific to Ruby, but it is the way the
language defines a ''type''.

martin