Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the core point of duck typing is how it is used. One uses method detection and introspection of the entity in order to know what to do with it, instead of declaring in advance what it <em>will</em> be ( where you know what to do with it ). </p> <p>It's probably more practical in OO languages, where primitives are not primitives, and are instead objects. </p> <p>I think the best way to sum it up, in variant type, an entity is/can be anything, and what it is is uncertain, as opposed to an entity only <em>looks</em> like anything, but you can work out what it is by asking it.</p> <p>Here's something I don't believe is plausible without ducktyping.</p> <pre><code>sub dance { my $creature = shift; if( $creature-&gt;can("walk") ){ $creature-&gt;walk("left",1); $creature-&gt;walk("right",1); $creature-&gt;walk("forward",1); $creature-&gt;walk("back",1); } if( $creature-&gt;can("fly") ){ $creature-&gt;fly("up"); $creature-&gt;fly("right",1); $creature-&gt;fly("forward",1); $creature-&gt;fly("left", 1 ); $creature-&gt;fly("back", 1 ); $creature-&gt;fly("down"); } else if ( $creature-&gt;can("walk") ) { $creature-&gt;walk("left",1); $creature-&gt;walk("right",1); $creature-&gt;walk("forward",1); $creature-&gt;walk("back",1); } else if ( $creature-&gt;can("splash") ) { $creature-&gt;splash( "up" ) for ( 0 .. 4 ); } if( $creature-&gt;can("quack") ) { $creature-&gt;quack(); } } my @x = (); push @x, new Rhinoceros ; push @x, new Flamingo; push @x, new Hyena; push @x, new Dolphin; push @x, new Duck; for my $creature (@x){ new Thread(sub{ dance( $creature ); }); } </code></pre> <p>Any other way would require you to put type restrictions on for functions, which would cut out different species, needing you to create different functions for different species, making the code really hellish to maintain. </p> <p>And that really sucks in terms of just trying to perform good choreography. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload