词条 | Flow-sensitive typing |
释义 |
In programming language theory, flow-sensitive typing (or flow typing) is a type system where the type of an expression depends on its position in the control flow. In statically typed languages, a type of an expression is determined by the types of the sub-expressions that compose it. However, in flow-sensitive typing, an expression's type may be updated to a more specific type if it follows a statement that validates its type. The type is determined by using type inference and type information is carried using algebraic data types. ExampleCeylonSee the following example in Ceylon which illustrates the concept: // Object? means the variable "name" is of type Object or else null void hello(Object? name) { if (is String name) { // "name" now has type String in this block print("Hello, ``name``!"); // and it is possible to call String methods on the variable print(" String.size is ``name.size``"); } else if (exists name) { // "name" now has type Object in this block print("Hello, object ``name``!"); } else { print("Hello, world!"); } } hello(null); hello(1); hello("John Doe"); Which outputs: KotlinSee this example in Kotlin: fun hello(obj: Any) { // A type cast fails if `obj` is not a String obj as String // Since the type cast did not fail, `obj` must be a String! val l = obj.length } hello("Mooooo") BenefitsThis technique coupled with type inference reduces the need for writing type annotations for all variables or to do type casting, like is seen with dynamic languages that use duck typing. It reduces verbosity and makes up for terser code, easier to read and modify. It can also help language implementers to provide faster implementations for dynamic languages by statically predicting the type of objects.[1] Finally, it increases type safety and can prevent problems due to null pointers, labeled by C.A.R. Hoare—the null reference inventor—as "the billion dollar mistake"[2] ImplementationsWhiley, created by David J. Pearce, was the first language to make use of flow-sensitive typing in 2009.[3][4]Since this introduction, other languages have made use of it, namely Ceylon,[5] Kotlin,[6][7] TypeScript[8] and Facebook Flow.[9] External references1. ^{{cite web | url=http://blog.jooq.org/2014/12/11/the-inconvenient-truth-about-dynamic-vs-static-typing | title=The Inconvenient Truth About Dynamic vs. Static Typing | publisher=blog.jooq.org | date=11 December 2014 | accessdate=11 March 2016 | author=Lukas Eder}} {{Data types}}2. ^{{cite web|url=http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare|title=Null References: The Billion Dollar Mistake|publisher=InfoQ.com|date=2009-08-25|author=Tony Hoare|quote=I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.}} 3. ^{{cite web | url=http://whiley.org/2010/09/22/on-flow-sensitive-types-in-whiley/ | title=On Flow-Sensitive Types in Whiley | publisher=whiley.org | date=22 September 2010 | accessdate=11 March 2016 | author=David J. Pearce}} 4. ^{{cite web | url=http://whiley.org/guide/typing/flow-typing/ | title=Whiley - Flow Typing | publisher=whiley.org | date=8 April 2012 | accessdate=11 March 2016 | author=David J. Pearce}} 5. ^{{cite web | url=http://ceylon-lang.org/documentation/1.2/introduction/#typesafe_null_and_flow_sensitive_typing | title=Ceylon - Quick introduction - Typesafe null and flow-sensitive typing | publisher=ceylon-lang.org | accessdate=11 March 2016}} 6. ^{{cite web | url=https://kotlinlang.org/docs/reference/null-safety.html | title=Null Safety | publisher=kotlinlang.org | accessdate=11 March 2016}} 7. ^{{cite web | url=https://kotlinlang.org/docs/reference/typecasts.html | title=Type Checks and Casts | publisher=kotlinlang.org | accessdate=11 March 2016}} 8. ^{{cite web | url=https://blogs.msdn.microsoft.com/typescript/2014/11/18/typescript-1-4-sneak-peek-union-types-type-guards-and-more | title=TypeScript 1.4 sneak peek: union types, type guards, and more | publisher=blogs.msdn.microsoft.com | date=18 November 2014 | accessdate=11 March 2016 | author=Ryan Cavanaugh}} 9. ^{{cite web | url=https://code.facebook.com/posts/1505962329687926/flow-a-new-static-type-checker-for-javascript | title=Flow, a new static type checker for JavaScript | publisher=code.facebook.com | date=18 November 2014 | accessdate=11 March 2016 | authors=Avik Chaudhuri, Basil Hosmer, Gabriel Levi}} 4 : Type systems|Data types|Program analysis|Type theory |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。