duck!

Michel Fortin michel.fortin at michelf.com
Sat Oct 16 20:04:45 PDT 2010


On 2010-10-16 22:02:35 -0400, Leandro Lucarella <luca at llucax.com.ar> said:

> I agree that "nametwo" is useless, but what you're proposing is plain
> dynamic typing, not duck typing. Duck-typing is what Go (and adaptTo)
> does. In dynamic languages you have to check for the methods existence
> at runtime just because is the only way to do it, but if you want to use
> a duck, you *need* to know that the object you're working with *can*
> quack().
> 
> That doesn't make dynamic typing a bad idea, it can be useful, but
> please don't name it "ducktype" as Andrei suggested.

What Go does is not duck typing, it's structural typing. It clearly has 
some of the benefits of duck typing and thus shares some similarities 
with it, but it's not the same.
<http://en.wikipedia.org/wiki/Structural_typing>


In my argumentation, I try to stick to Wikipedia's definition of duck typing:
<http://en.wikipedia.org/wiki/Duck_typing>

> In computer programming with object-oriented programming languages, 
> duck typing is a style of dynamic typing in which an object's current 
> set of methods and properties determines the valid semantics, rather 
> than its inheritance from a particular class or implementation of a 
> specific interface.
> 
> [...]
> 
> In duck typing, one is concerned with just those aspects of an object 
> that are used, rather than with the type of the object itself.
> 
> [...]
> 
> Duck typing is aided by habitually not testing for the type of 
> arguments in method and function bodies, relying on documentation, 
> clear code, and testing to ensure correct use. Users of statically 
> typed languages new to dynamically typed languages are usually tempted 
> to add such static (before run-time) type checks, defeating the 
> benefits and flexibility of duck typing, and constraining the 
> language's dynamism.

Read twice that last sentence. Clearly, statically implementing an 
interface is not what duck-typing is about. It could perhaps qualify as 
some sort structural typing, but it's better known as the adapter 
pattern.
<http://en.wikipedia.org/wiki/Adapter_pattern>


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list