Duck typing, must it be dynamic?

后端 未结 8 1159
花落未央
花落未央 2020-12-13 09:55

Wikipedia used to say* about duck-typing:

In computer programming with object-oriented programming languages, duck typing is a style of dynamic

8条回答
  •  余生分开走
    2020-12-13 10:26

    There are always going to be examples from some programming languages that violate some definitions of various terms. For example, ActionScript supports doing duck-typing style programming on instances that are not technically dynamic.

    var x:Object = new SomeClass();
    if ("begin" in x) {
        x.begin();
    }
    

    In this case we tested if the object instance in "x" has a method "begin" before calling it instead of using an interface. This works in ActionScript and is pretty much duck-typing, even though the class SomeClass() may not itself be dynamic.

提交回复
热议问题