什么是弱类型语言的益处(和缺点)? [英] What are the benefits (and drawbacks) of a weakly typed language?

查看:240
本文介绍了什么是弱类型语言的益处(和缺点)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的大风扇,这显然是一个非常弱类型语言。我认识的一些好处包括动态和不断变化等变量类型的一般独立性。

I'm a big fan of PHP and it's obviously a very weakly-typed language. I realize some of the benefits include the general independence of changing variable types on the fly and such.

什么我想了解一下是缺点。你可以滚蛋像C强类型的语言,否则你不能从一个弱类型的人喜欢PHP得到的是什么?还与类型设置(如双($变量)),人们可以说,即使是弱类型语言可以充当就像一个强类型之一。

What I'm wondering about are the drawbacks. What can you get out of a strongly-typed language like C that you otherwise can't get from a weakly-typed one like PHP? Also with type setting (like double($variable)), one could argue that even a weakly-typed language can act just like a strongly-typed one.

所以。弱型。什么是一些好处,我不包括?更重要的是,有什么缺点?

So. Weak-type. What are some benefits I didn't include? More importantly, what are the drawbacks?

推荐答案

的引用优势的静态的打字是,有整个班级在编译时捕获错误的,即无法达到运行时间。例如,如果你有一个静态类型的类或接口作为函数参数,那么你织补以及不会在错误类型的对象意外地传递(没有明确的和不正确的投,那是)。

The cited advantage of static typing is that there are whole classes of errors caught at compile time, that cannot reach runtime. For example, if you have a statically-typed class or interface as a function parameter, then you are darn well not going to accidentally pass in an object of the wrong type (without an explicit and incorrect cast, that is).

当然,这并不能阻止你在正确的类型,或者你给它的正确功能的接口实现的错误的对象传递,但他们做了错误的事情。此外,如果你有100%code覆盖面,说PHP / Python的/等乡亲,谁在乎你是否在编译时或运行时?

Of course, this doesn't stop you passing in the wrong object of the right type, or an implementation of an interface where you've given it the right functions but they do the wrong things. Furthermore, if you have 100% code coverage, say the PHP/Python/etc folks, who cares whether you catch the error at compile time or at run time?

就个人而言,我已经与静态类型语言的欢乐时光,并在语言的乐趣次无。这是很少的决定性问题,因为我从来没有过两种语言它比同类打字等相同,有担心,通常更重要的事情之间做出选择。我发现,当我使用静态类型语言我特意瘦肉精编译器,试图写code以这样一种方式,如果它是错误的,它不会编译。例如有,你可以通过在一个地方的变化,然后固定而产生的所有的编译错误执行某些refactors,重复,直到干净的编译。通过运行一个完整的测试套件多次做同样的事情可能不是很实用。但它是前所未闻的不是IDE来在其他语言一样refactors自动化,或测试,以迅速完成,所以这是一个什么样的便利问题,而不是什么是可能的。

Personally, I've had fun times in languages with static typing, and fun times in languages without. It's rarely the deciding issue, since I've never had to choose between two languages which are identical other than their kind of typing and there are normally more important things to worry about. I do find that when I'm using statically typed languages I deliberately "lean on the compiler", trying to write code in such a way that if it's wrong, it won't compile. For instance there are certain refactors which you can perform by making a change in one place, and then fixing all the compilation errors which result, repeat until clean compile. Doing the same thing by running a full test suite several times might not be very practical. But it's not unheard-of for IDEs to automate the same refactors in other languages, or for tests to complete quickly, so it's a question of what's convenient, not what's possible.

谁拥有超越便利正当关切和编码风格preference的人们都在code正确性的形式证明工作的人。我的无知IM pression是静态类型推导可以做的大多数(但不是全部)显式静态类型确实,节省可观的磨损键盘上的工作。所以的如果的静态类型部队的人来写code的方式,使得它更容易证明,然后的出现很可能是某事它从POV。我说如果:我不知道,这并不是因为如果大多数人证明自己的静态类型code反正

The folks who have a legitimate concern beyond convenience and coding style preference are the ones working on formal proofs of the correctness of code. My ignorant impression is that static type deduction can do most (but not all) of the work that explicit static typing does, and saves considerable wear and tear on the keyboard. So if static typing forces people to write code in a way that makes it easier to prove, then there could well be something to it from that POV. I say "if": I don't know, and it's not as if most people prove their statically-typed code anyway.

动态和不断变化的变量类型,例如

我觉得可疑的价值的。它总是如此诱人像做(的Python / Django的):

I think that's of dubious value. It's always so tempting to do something like (Python/Django):

user = request.GET['username']
# do something with the string variable, "user"
user = get_object_or_404(User,user)
# do something with the User object variable, "user"

不过说真的,如果同一个名字被用于不同的东西在函数中?也许。可能不会。 再利用,例如,在静态类型语言其他事情整型变量没有大规模鼓励无论是。欲望不要有想简洁明了的描述变量名,时间应该不会覆盖不含糊code中的欲望大概95%...

But really, should the same name be used for different things within a function? Maybe. Probably not. "Re-using", for example, integer variables for other things in statically typed languages isn't massively encouraged either. The desire not to have to think of concise, descriptive variable names, probably 95% of the time shouldn't override the desire for unambiguous code...

顺便说一句,通常的打字意味着发生的隐式类型转换,而强大的的打字意味着他们不这样做。按照这个定义,C是弱至于算术类型而言类型的,所以我认为这不是你的意思。我认为这是普遍认为,全强类型更是一个滋扰而不是帮助,而全弱类型(什么都可以转换成任何东西)是大多数语言荒谬的。所以,问题是有关于有多少和你的前code变得太难搞清楚是可以容忍的什么隐式转换。又见,在C ++中,正在进行的困难在决定是否实施转换操作符和非显性单参数构造。

Btw, usually weak typing means that implicit type conversions occur, and strong typing means they don't. By this definition, C is weakly typed as far as the arithmetic types are concerned, so I assume that's not what you mean. I think it's widely considered that full strong typing is more of a nuisance than a help, and "full weak typing" (anything can be converted to anything else) is nonsensical in most languages. So the question there is about how many and what implicit conversions can be tolerated before your code becomes too difficult to figure out. See also, in C++, the ongoing difficulty in deciding whether to implement conversion operators and non-explicit one-arg constructors.

这篇关于什么是弱类型语言的益处(和缺点)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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