没有鸭子类型的动态类型?
我习惯了动态类型,这意味着在运行时检查对象/非面向对象结构的类型信息并抛出某种类型错误,即如果它像鸭子一样嘎嘎叫,那么它就是鸭子。是否有不同类型的动态类型(请详细说明)。
I'm used to dynamic typing meaning checking for type info of object/non object oriented structure at runtime and throwing some sort of type error, ie if it quacks like a duck its a duck. Is there a different type of dynamic typing (please go into details).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,绝对是。鸭子类型是一种习语,表示此时值的类型基于它现在拥有的字段和方法。动态类型只是说类型与运行时值相关联,而不是与静态变量和参数相关联。两者是有区别的,可以使用后者而不使用前者。
例如,如果您使用 PHP 进行编程并将自己限制为基本类型而不使用 OO,那么您将使用动态类型而不使用鸭子类型。
Yes, absolutely. Duck-typing is an idiom which says that the type of a value at this moment in time is based on the fields and methods that it has right now. Dynamic typing just says that types are associated with run-time values, not with static variables and parameters. There is a difference between the two, and you can use the latter without the former.
For example, if you programmed in PHP and limited yourself to the basic types without using OO, then you would be using dynamic typing without using duck-typing.
不,动态类型是指值具有类型但变量没有,因此大多数类型检查是在运行时完成的。因此,基本上,如果该值像鸭子一样行走或嘎嘎叫,那么它就是鸭子,否则会引发错误。鸭子类型实际上只是描述了动态类型的一个功能,确保它是类型安全的(即,只有变量
foo
' 具有正确的属性或可以执行该方法时,方法才会运行)。No, dynamic typing is when values have type but variables do not, so most type checking is done at runtime. So, basically, if the value walks or quacks like a duck, it's a duck, else an error is thrown. Duck typing really just describes a feature of dynamic typing that ensures it will be typesafe (i.e. a method will only run if variable
foo
'has the right attribute or can execute that method).