鸭式打字与静态打字有什么优势? [英] What are some advantages of duck-typing vs. static typing?

查看:108
本文介绍了鸭式打字与静态打字有什么优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Groovy和尝试更多,我正试图围绕在Groovy中实现事物的利弊,我不能/不会在Java中做这件事。动态编程对我来说仍然只是一个概念,因为我已经深深地沉浸在静态和强类型语言中。

Groovy让我能够鸭式,但我无法真正看到它的价值。鸭子打字如何比静态打字更有效率?在我的代码实践中,我可以做什么样的事情来帮助我理解它的好处?



我在考虑Groovy时问这个问题,但我明白它不是必然是一个Groovy问题,所以我欢迎来自每个代码阵营的答案。

解决方案

接下来,哪个更好:EMACS还是vi?这是一场正在进行的宗教战争。



想想这样:任何正确的程序,如果语言是静态键入。静态类型的作用是让编译器有足够的信息在编译时而不是运行时检测类型不匹配。这可能是一个烦恼,如果你做增量类型的编程,虽然(我认为),如果你清楚地思考你的程序它并不重要;另一方面,如果你正在构建一个非常大的程序,例如操作系统或电话交换机,数十或数百人或数千人在其上工作,或者具有非常高的可靠性要求,那么让他的编译器能够为您检测一大类问题,而不需要测试用例就可以执行正确的代码路径。



动态类型不是一个新的不同的东西:C ,例如,是有效的动态类型,因为我总是可以将 foo * 转换为 bar * 。这只是意味着当我的地址真的指向一个 foo时,作为C程序员,我从不使用适用于 bar * 的代码的责任* 。但是由于大型程序的问题,C增长了像lint(1)这样的工具,使用 typedef 增强了它的类型系统,最终在C ++中开发了一个强类型变体。 (当然,C ++反过来也开发了强大的打字方法,包括所有类型的类型和泛型/模板,以及RTTI。另外一件事, ---不要混淆敏捷编程和动态语言。敏捷编程是关于人们工作的方式在一个项目中一起工作:项目能否适应不断变化的需求以满足客户的需求,同时为程序员保持人性化的环境?可以使用动态类型语言来完成,而且通常是,因为它们可以提高生产力(例如Ruby ,Smalltalk),但它可以完成,已经成功完成,使用C语言甚至汇编。事实上,拉力赛开发<甚至使用敏捷方法(特别是SCRUM)来做市场营销和文档。

I'm researching and experimenting more with Groovy and I'm trying to wrap my mind around the pros and cons of implementing things in Groovy that I can't/don't do in Java. Dynamic programming is still just a concept to me since I've been deeply steeped static and strongly typed languages.

Groovy gives me the ability to duck-type, but I can't really see the value. How is duck-typing more productive than static typing? What kind of things can I do in my code practice to help me grasp the benefits of it?

I ask this question with Groovy in mind but I understand it isn't necessarily a Groovy question so I welcome answers from every code camp.

解决方案

Next, which is better: EMACS or vi? This is one of the running religious wars.

Think of it this way: any program that is correct, will be correct if the language is statically typed. What static typing does is let the compiler have enough information to detect type mismatches at compile time instead of run time. This can be an annoyance if your doing incremental sorts of programming, although (I maintain) if you're thinking clearly about your program it doesn't much matter; on the other hand, if you're building a really big program, like an operating system or a telephone switch, with dozens or hundreds or thousands of people working on it, or with really high reliability requirements, then having he compiler be able to detect a large class of problems for you without needing a test case to exercise just the right code path.

It's not as if dynamic typing is a new and different thing: C, for example, is effectively dynamically typed, since I can always cast a foo* to a bar*. It just means it's then my responsibility as a C programmer never to use code that is appropriate on a bar* when the address is really pointing to a foo*. But as a result of the issues with large programs, C grew tools like lint(1), strengthened its type system with typedef and eventually developed a strongly typed variant in C++. (And, of course, C++ in turn developed ways around the strong typing, with all the varieties of casts and generics/templates and with RTTI.

One other thing, though --- don't confuse "agile programming" with "dynamic languages". Agile programming is about the way people work together in a project: can the project adapt to changing requirements to meet the customers' needs while maintaining a humane environment for the programmers? It can be done with dynamically typed languages, and often is, because they can be more productive (eg, Ruby, Smalltalk), but it can be done, has been done successfully, in C and even assembler. In fact, Rally Development even uses agile methods (SCRUM in particular) to do marketing and documentation.

这篇关于鸭式打字与静态打字有什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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