开发者

How is called use of duck typing

开发者 https://www.devze.com 2023-02-05 23:59 出处:网络
How should I call process of using duck typing? What should I say \"I\'m performing duck typi开发者_运维百科ng\" or \"I\'m checking if it\'s a duck:)\".

How should I call process of using duck typing? What should I say "I'm performing duck typi开发者_运维百科ng" or "I'm checking if it's a duck:)".

Duck checking doesn't sounds good and type checking is just plain wrong name for it.


I am using duck typing, I would say. It's along the same kind of lines as 'I am using static typing', though they are different things.


I think context matters here, are we talking about inline comments or overall design documents?

For design documents I think it's quite reasonable to say I am using duck typing.

At a given point in the code I'm not sure that it's useful to say that. Consider, using some fictional language

 function wibble(anObject) {

      // what do we say here? 
      anObject.computeMedianValue();

      anObject.render(device);
 }

First note that we don't actually check it's a duck, in the examples I see the code just goes

 obj.quack()

The program structure usually arranges that only things that can quack get passed down. I don't see many examples of

if (obj can quack )
  obj.quack()
else if ( obj can grunt)
  obj.grunt()

that wouldn't be very polymorphic, but even if we did want to check the capabilities we very much do not write code

if ( obj isA duck)

the whole idea of duck typing is that we consider the specific capabilities we need in the current context - walk (like a duck) , quack (like a duck) - not "is it a duck?"

So in the code I would not particularly reference duck typing, any more than I sprinkle I'm doing object orientation around a standard Java program. The comment might say why you know that that object can compute a median, or what to do if it can't.


Duck Typing

The name comes from the phrase:

If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

https://en.wikipedia.org/wiki/Duck_test

Which implies that if it has all the relevant attributes of a duck, why bother doing careful tests to check if it something other than a duck.

It may be something other than a duck, but most people won't care, because it is duck-like in those ways that matter right now.

Duck typing is similar in this way: Why bother controlling the type of parameters and variables in order to restrict what operations the user can perform.

It doesn't matter what types you pass to a function as long as the operations still work on whatever type it really is.

If you can add two types, who cares what number type they are?

If the object you pass to the paginateResult() function has a query(), and a get_next_result() method what does it matter what type it is?

In a statically typed language, the object would have to be a subclass of queryResult or implement a paginates interface.

https://en.wikipedia.org/wiki/Duck_typing

0

精彩评论

暂无评论...
验证码 换一张
取 消