dynamic classes and duck typing

Michal Minich michal.minich at gmail.com
Wed Dec 2 08:52:40 PST 2009


Hello bearophile,

> But in dynamic code you don't almost never assert that a variable is
> an int; you assert that 'a' is able to do its work where it's used. So
> 'a' can often be an int, decimal, a multiprecision long, a GMP
> multiprecision, or maybe even a float. What you care of it not what a
> is but if does what it has to, so you care if it quacks :-) That's
> duck typing.

Yes that's duck typing: "assert that 'a' is able to do its work where it's 
used" (function with required signature exists)

Interfaces in OOP, or type classes in Haskell are here to "assert that 'a' 
is intended to work where it's used" (type is some implementation of the 
required concept (int/long/bigint))

both have its place :)

Note that duck typing need not to be only dynamic, it can also happen at 
compile time - ranges in D checks if some functions are specified for "object" 
at compile time.





More information about the Digitalmars-d mailing list