1、何谓Duck Typing [Ruby]

问题:何谓Duck Typing? Duck Typing是否是动态语言特有的,静态类型语言中是否有类似的实现?

 

回答:

 

  Duck Typing 是一种实用主义的面向对象方法,它对对象没有约定,只是在运行时进行检查,如果作为参数的对象拥有需要的方法和属性,就符合使用的要求。

 

  “当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来像也鸭子,那个这只鸟就可以被称为鸭子。”

 

 

  这是一条简单实用而又有些过于感性的原则。

 

  Duck Typing 同样可以实现在静态语言中(比如BOO、C#),只不过要在编译时识别和指定这些动态类。

 

Duck Typing

 

以下资料转载自:http://en.wikipedia.org/wiki/Duck_typing

Duck typing

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In computer programming, 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. The name of the concept refers to the duck test, attributed to James Whitcomb Riley (see History below), 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."

In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. For example, in a non-duck-typed language, one can create a function that takes an object of type Duck and calls that object's walk and quack methods. In a duck-typed language, the equivalent function would take an object of any type and call that object's walk and quack methods. If the object does not have the methods that are called then the function signals a run-time error. It is this action of any object having the correct walk and quack methods being accepted by the function that evokes the quotation and hence the name of this form of typing.

Duck typing is aided by habitually not testing for the type of arguments in method and function bodies, relying on documentation, clear code, and testing to ensure correct use. Users of statically typed languages new to dynamically typed languages are usually tempted to add such static (before run-time) type checks, defeating the benefits and flexibility of duck typing, and constraining the language's dynamism.

 

Duck typing in statically typed languages

Certain usually statically typed languages such as Boo and the version 4 release of C# have extra type annotations[1][2] that instruct the compiler to arrange for type checking of classes to occur at run-time rather than compile time, and include run-time type checking code in the compiled output. Such additions allow the language to enjoy most of the benefits of duck typing with the only drawback being the need to identify and specify such dynamic classes at compile time.

 

Comparison with other type systems

[edit] Structural type systems

Duck typing is similar to but distinct from structural typing. Structural typing determines type compatibility and equivalence by a type's structure, whereas duck typing determines type compatibility by only that part of a type's structure that is accessed during run time. The Objective Caml programming language uses a structural type system.

[edit] Interfaces

Interfaces can provide some of the benefits of duck typing but duck typing is distinct in that no explicit interface is defined. For example, if a third party Java library implements a class you are not allowed to modify, you cannot use an instance of the class in place of an interface you've defined yourself, whereas duck typing would allow this.

[edit] Templates or generic types

Template functions or methods apply the duck test in a static typing context; this brings all the advantages and disadvantages of static versus dynamic type checking in general. Duck typing can also be more flexible in that only the methods actually called at run time need to be implemented, while templates require implementation of all methods that cannot be proven unreachable at compile time.

Examples include the language C++ with templates, and the Java language's generics.

 

其他相关:JavaScript的Duck Typing 实现 http://robinqu.javaeye.com/blog/426582

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值