Duck typing, must it be dynamic?

后端 未结 8 1130
花落未央
花落未央 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条回答
  •  Happy的楠姐
    2020-12-13 10:21

    Structural Type System

    A structural type system compares one entire type to another entire type to determine whether they are compatible. For two types A and B to be compatible, A and B must have the same structure – that is, every method on A and on B must have the same signature.

    Duck Typing

    Duck typing considers two types to be equivalent for the task at hand if they can both handle that task. For two types A and B to be equivalent to a piece of code that wants to write to a file, A and B both must implement a write method.

    Summary

    Structural type systems compare every method signature (entire structure). Duck typing compares the methods that are relevant to a specific task (structure relevant to a task).

提交回复
热议问题