diff options
author | Michael Witten <mfwitten@gmail.com> | 2009-04-07 14:59:23 -0500 |
---|---|---|
committer | Yves Orton <demerphq@gmail.com> | 2009-04-07 23:41:31 +0200 |
commit | c98e0e85db98f82e6cf495b56dc57b4df0e3d51d (patch) | |
tree | 2d2fb2b89022fb3f8d77d483f2a46567d0d9feb5 /pod/perlboot.pod | |
parent | 2ee409f9fc5c2201b0d49c1f13f3c2ae8804305b (diff) | |
download | perl-c98e0e85db98f82e6cf495b56dc57b4df0e3d51d.tar.gz |
Docs: Quick explanation of duck typing and a warning
Signed-off-by: Michael Witten <mfwitten@gmail.com>
Diffstat (limited to 'pod/perlboot.pod')
-rw-r--r-- | pod/perlboot.pod | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/pod/perlboot.pod b/pod/perlboot.pod index afe9837986..5a60d628ad 100644 --- a/pod/perlboot.pod +++ b/pod/perlboot.pod @@ -296,15 +296,24 @@ which results in: [but you can barely hear it!] Here, C<Mouse> has its own speaking routine, so C<< Mouse->speak >> -doesn't immediately invoke C<< Animal->speak >>. This is known as -"overriding". In fact, we didn't even need to say that a C<Mouse> was -an C<Animal> at all, since all of the methods needed for C<speak> are -completely defined with C<Mouse>. - -But we've now duplicated some of the code from C<< Animal->speak >>, -and this can once again be a maintenance headache. So, can we avoid -that? Can we say somehow that a C<Mouse> does everything any other -C<Animal> does, but add in the extra comment? Sure! +doesn't immediately invoke C<< Animal->speak >>. This is known as +"overriding". In fact, we don't even need to say that a C<Mouse> is +an C<Animal> at all, because all of the methods needed for C<speak> are +completely defined for C<Mouse>; this is known as "duck typing": +"If it walks like a duck and quacks like a duck, I would call it a duck" +(James Whitcomb). However, it would probably be beneficial to allow a +closer examination to conclude that a C<Mouse> is indeed an C<Animal>, +so it is actually better to define C<Mouse> with C<Animal> as its base +(that is, it is better to "derive C<Mouse> from C<Animal>"). + +Moreover, this duplication of code could become a maintenance headache +(though code-reuse is not actually a good reason for inheritance; good +design practices dictate that a derived class should be usable wherever +its base class is usable, which might not be the outcome if code-reuse +is the sole criterion for inheritance. Just remember that a C<Mouse> +should always act like an C<Animal>). + +So, let's make C<Mouse> an C<Animal>! First, we can invoke the C<Animal::speak> method directly: |