鸭子打字与旧的“变种”类型和/或界面有何不同? [英] How is duck typing different from the old 'variant' type and/or interfaces?

查看:200
本文介绍了鸭子打字与旧的“变种”类型和/或界面有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直看到鸭子打字这个短语,甚至还遇到了一两个代码示例。我太忙了 lazy 做我自己的研究,有人可以简单告诉我:




  • 差异在'duck type'和old-skool'变体类型'之间,和

  • 提供了一个例子,我可能更喜欢鸭子打字而不是变种打字,

  • 提供一些我 使用鸭子打字来完成的事情的例子?





<通过怀疑这个新结构的力量,我不是故意看起来像鸡一样,我不会因为拒绝做研究而避开这个问题,但是我正在喋喋不休地讨论我所做的所有植绒炒作最近看到它。它看起来像是没有打字(也就是动态打字),所以我没有立即看到它们的优势。



ADDENDUM:谢谢你到目前为止的例子。在我看来,使用像'O-> can(Blah)'这样的东西相当于做反射查找(可能不便宜),和/或与编译器可能说的(O是IBlah)大致相同能够检查你,但后者的优点是区分我的IBlah界面和你的IBlah界面,而其他两个没有。当然,每种方法都有很多微小的界面会变得混乱,但是那样可以检查很多单独的方法...



...所以我再也没有得到它。在一个全新的麻袋中,它是一个梦幻般的节省时间,还是同样的老东西? 需要鸭子打字的示例在哪里?

解决方案

简单的答案是变体是弱类型的鸭子打字是强类型的。



鸭子打字可以很好地总结为如果它像鸭子一样走路,看起来像鸭子,就像鸭子一样,那就是鸭子。计算机科学术语认为duck是以下接口。

  interface IDuck {
void Quack();
}

现在让我们来看看Daffy

  class Daffy {
void Quack(){
Console.WriteLine(Thatsssss dispicable !!!!);
}
}

在这种情况下,Daffy实际上不是IDuck。然而它就像一只鸭子。为什么让Daffy实施IDuck,因为很明显Daffy实际上是一只鸭子。



这就是Duck输入的地方。它允许在具有IDuck和IDuck引用的所有行为的任何类型之间进行类型安全转换。

  IDuck d = new Daffy(); 
d.Quack();

现在可以在d上调用Quack方法,并确保完全类型安全。此赋值或方法调用中不存在运行时类型错误的可能性。


I keep seeing the phrase "duck typing" bandied about, and even ran across a code example or two. I am way too lazy busy to do my own research, can someone tell me, briefly:

  • the difference between a 'duck type' and an old-skool 'variant type', and
  • provide an example of where I might prefer duck typing over variant typing, and
  • provide an example of something that i would have to use duck typing to accomplish?

I don't mean to seem fowl by doubting the power of this 'new' construct, and I'm not ducking the issue by refusing to do the research, but I am quacking up at all the flocking hype i've been seeing about it lately. It looks like no typing (aka dynamic typing) to me, so I'm not seeing the advantages right away.

ADDENDUM: Thanks for the examples so far. It seems to me that using something like 'O->can(Blah)' is equivalent to doing a reflection lookup (which is probably not cheap), and/or is about the same as saying (O is IBlah) which the compiler might be able to check for you, but the latter has the advantage of distinguishing my IBlah interface from your IBlah interface while the other two do not. Granted, having a lot of tiny interfaces floating around for every method would get messy, but then again so can checking for a lot of individual methods...

...so again i'm just not getting it. Is it a fantastic time-saver, or the same old thing in a brand new sack? Where is the example that requires duck typing?

解决方案

The simple answer is variant is weakly typed while duck typing is strongly typed.

Duck typing can be summed up nicely as "if it walks like a duck, looks like a duck, acts like a duck, then it's a duck." It computer science terms consider duck to be the following interface.

interface IDuck {
  void Quack();
}

Now let's examine Daffy

class Daffy {
  void Quack() {
    Console.WriteLine("Thatsssss dispicable!!!!");
  }
}

Daffy is not actually an IDuck in this case. Yet it acts just like a Duck. Why make Daffy implement IDuck when it's quite obvious that Daffy is in fact a duck.

This is where Duck typing comes in. It allows a type safe conversion between any type that has all of the behaviors of a IDuck and an IDuck reference.

IDuck d = new Daffy();
d.Quack();

The Quack method can now be called on "d" with complete type safety. There is no chance of a runtime type error in this assignment or method call.

这篇关于鸭子打字与旧的“变种”类型和/或界面有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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