鸭子打字,必须动态吗? [英] Duck typing, must it be dynamic?

查看:153
本文介绍了鸭子打字,必须动态吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Wikipedia曾经对* a 鸭型:

Wikipedia used to say* about 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.

(* *注:自发布此问题以来,已对Wikipedia文章进行了编辑,以删除动态"一词.)

它说明了结构类型:

结构类型系统(或 基于属性的类型系统)是一个主要 类型系统的类别,其中类型 兼容性和等效性是 由类型的结构决定 而不是通过明确的声明.

A structural type system (or property-based type system) is a major class of type system, in which type compatibility and equivalence are determined by the type's structure, and not through explicit declarations.

它将结构子类型与鸭类类型进行了对比:

It contrasts structural subtyping with duck-typing as so:

[结构系统]与 ...鸭子打字,其中只有 在以下位置访问的结构的一部分 检查运行时的兼容性.

[Structural systems] contrasts with ... duck typing, in which only the part of the structure accessed at runtime is checked for compatibility.

但是,在我看来,术语鸭型至少是直观地包含了结构型子型系统.实际上,维基百科说:

However, the term duck-typing seems to me at least to intuitively subsume structural sub-typing systems. In fact Wikipedia says:

概念名称[鸭式输入] 是指鸭子测试,归因于 詹姆斯·惠特科姆·莱利(James Whitcomb Riley) 如下:当我看到一只小鸟在走路时 像鸭子一样游泳,像鸭子一样游泳 嘎嘎叫鸭子,我叫那只鸟 鸭子."

The name of the concept [duck-typing] refers to the duck test, attributed to James Whitcomb Riley which may be phrased as follows: "when I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck."

所以我的问题是:为什么不能将结构子类型称为鸭型?甚至存在动态类型的语言,这些语言也无法归类为鸭子类型吗?

So my question is: why can't I call structural subtyping duck-typing? Do there even exist dynamically typed languages which can't also be classified as being duck-typed?

后记:

由于在reddit.com上名为 daydreamdrunk 的某人,所以

As someone named daydreamdrunk on reddit.com so eloquently put-it "If it compiles like a duck and links like a duck ..."

后记

许多答案似乎基本上只是在重新表达我在这里已经引用的内容,而没有解决更深层的问题,这就是为什么不使用术语鸭式打字"来涵盖动态类型和结构子打字?如果您只想谈论鸭子类型而不是结构子类型,则可以将其命名为:动态成员查找.我的问题是,鸭式打字一词对我说的没什么,这仅适用于动态语言.

Many answers seem to be basically just rehashing what I already quoted here, without addressing the deeper question, which is why not use the term duck-typing to cover both dynamic typing and structural sub-typing? If you only want to talk about duck-typing and not structural sub-typing, then just call it what it is: dynamic member lookup. My problem is that nothing about the term duck-typing says to me, this only applies to dynamic languages.

推荐答案

C ++和D模板是动态鸭式输入的完美示例.绝对是:

C++ and D templates are a perfect example of duck typing that is not dynamic. It is definitely:

键入其中 对象的当前方法集和 属性确定有效 语义,而不是其继承 来自特定类别或 具体实施 界面.

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.

您没有明确指定类型必须实例化模板的接口.它只需要具有模板定义中使用的所有功能.但是,所有内容都会在编译时解析,然后编译为原始的,难以理解的十六进制数.我称之为编译时鸭子输入".我已经从这种思维方式编写了整个库,隐式模板实例化是编译时的鸭子输入,并认为它是其中最不被人赏识的功能之一.

You don't explicitly specify an interface that your type must inherit from to instantiate the template. It just needs to have all the features that are used inside the template definition. However, everything gets resolved at compile time, and compiled down to raw, inscrutable hexadecimal numbers. I call this "compile time duck typing". I've written entire libraries from this mindset that implicit template instantiation is compile time duck typing and think it's one of the most under-appreciated features out there.

这篇关于鸭子打字,必须动态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆