Note that there are some explanatory texts on larger screens.

plurals
  1. POHow is duck typing different from the old 'variant' type and/or interfaces?
    text
    copied!<p>I keep seeing the phrase "duck typing" bandied about, and even ran across a code example or two. I am way too <strike>lazy</strike> busy to do my own research, can someone tell me, briefly:</p> <ul> <li>the difference between a 'duck type' and an old-skool 'variant type', and </li> <li>provide an example of where I might prefer duck typing over variant typing, and</li> <li>provide an example of something that i would <em>have</em> to use duck typing to accomplish?</li> </ul> <p><img src="https://regmedia.co.uk/2007/05/03/cartoon_duck.jpg" alt="duck typing illustration courtesy of The Register"></p> <p>I don't mean to seem fowl by doubting the power of this 'new' construct, and I'm not ducking the issue by refusing to do the research, but I am quacking up at all the flocking hype i've been seeing about it lately. It looks like <em>no</em> typing (aka dynamic typing) to me, so I'm not seeing the advantages right away.</p> <p>ADDENDUM: Thanks for the examples so far. It seems to me that using something like 'O->can(Blah)' is equivalent to doing a reflection lookup (which is probably not cheap), and/or is about the same as saying (O is IBlah) which the compiler might be able to check for you, but the latter has the advantage of distinguishing my IBlah interface from your IBlah interface while the other two do not. Granted, having a lot of tiny interfaces floating around for every method would get messy, but then again so can checking for a lot of individual methods...</p> <p>...so again i'm just not getting it. Is it a fantastic time-saver, or the same old thing in a brand new sack? Where is the example that <em>requires</em> duck typing?</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