psmay.com
域名年龄: 18年6个月21天HTTP/1.1 200 OK 访问时间:2012年09月19日 04:11:03 X-Pingback: http://psmay.com/xmlrpc.php 动作:Accept-Encoding 文件大小:81455 类型:text/html; charset=UTF-8 网站编码:UTF-8
—psmay.com Human Being, Aspiring Polymath HomeContactPublic Key NotaryWish ListProjectsBack BurnerAnalog PWMCNC machineMicrocontroller breadboard utility headerParanoid Password BuddySuper BoxTrickyEnglish Type Safety and the Explicit Cast 2012 August 24Concept Leave a Comment I think statically typed programming languages are great. Why? Because in just about every dynamically typed language I’ve ever used in earnest—JavaScript, for example—at some point I’ve had to write a lot of code to verify that a value looks like a certain type (one often can’t get a full guarantee) in order to make assertions about the theoretical correctness and consistency of a program. Is that a string? An array? A number? I can’t tell you until I’ve looked. For better or worse, JavaScript will allow absolutely anything (or nothing at all!) as a parameter. Sure, it’s a simple way to implement polymorphic functions without a whole lot of fuss…at first. But most functions I write are intended to work on a set of values that is much, much smaller than the set of values it is not designed to work on. At a medium-to-high level, this is a non-issue with static typing. I specify a function to operate on values of specific types, and the program won’t even compile if someone tries to pass something different. Apologists for dynamic languages have pointed to the verbosity and inflexibility of having to specify the type of every single thing, but advances such as type inferencedetermining the type of something based on how it is usedand type-safe genericsfunctions and objects that can operate on arguments of arbitrary types if those types fit general criteriahave made it easier for programmers to convey all of the intended meaning without sacrificing too much flexibility or writing too many more words than necessary. Another important point to consider is the failure mode of a program that isn’t written correctly. Inconsistencies in the use of values and types tends to be a compile-time error for static languages; they are caught before the program is even run. In a dynamic language like JavaScript, no static types are enforced per se, but using a value as if it is something it’s not can result in a run-time error in the best case[1] and instead often “succeeds”, producing unexpected results [2]. Of course, I don’t like the bits of the static languages that continue to allow type-unsafe behavior. In my opinion, the major violators are: Static casts, such as Java/C# String s = (String)o for some Object o Instance-of as a boolean operator (more of an accomplice than a criminal) Inheritance-based polymorphism used in place of tagged unions Null being an allowed (and often non-disallowable) value for any reference type Starting with the last one: It really seems strange to me that we automatically allow null to be passed as an object, even if it is an artifact of C (a reference being implemented as a
© 2010 - 2020 网站综合信息查询 同IP网站查询 相关类似网站查询 网站备案查询网站地图 最新查询 最近更新 优秀网站 热门网站 全部网站 同IP查询 备案查询
2025-02-22 04:55, Process in 0.0041 second.