Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types...
Transcript of Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types...
![Page 1: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/1.jpg)
Optional Typing in Dart: Purity vs. PracticeGilad Bracha Google
![Page 2: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/2.jpg)
Optional Types
A type system is optional if
It has no effect on the run time semantics
It is syntactically optional
![Page 3: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/3.jpg)
Optional Types
A type system is optional if
It has no effect on the run time semantics
It is syntactically optional
![Page 4: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/4.jpg)
Runtime dependent on Type System
Type RulesSemantics
![Page 5: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/5.jpg)
Runtime not dependent on Type System
Type RulesSemantics
![Page 6: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/6.jpg)
Dart Types Overview
Nominal
Interface-based
Unsound for multiple reasons
![Page 7: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/7.jpg)
Dart Types Overview
Two Modes:
Checked ~ Gradual. Check runtime type against declaration at assignment, parameter passing, function return
Production ~ Optional. Type annotations have no effect.
![Page 8: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/8.jpg)
Checked Mode undermines Optional Typing
Code will be used in both checked and production modes
Checked mode gives annotation meaning
Hence annotations are not truly optional
But checked mode is very useful
![Page 9: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/9.jpg)
Controlling Checked Mode
One needs finer grain control over checked mode
Ideally, one could choose on a library or method basis whether to do the dynamic checks
Checked mode should be a feature of the tooling, not the language
![Page 10: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/10.jpg)
Tangent: PX not PL
Programming experience (PX) is what matters
PX holistically integrates language, tools, libraries, performance etc.
Separating PL is a very useful level of abstraction, but one needs to know when to do.
![Page 11: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/11.jpg)
Pluggable Types
If type systems are optional, one can treat them as plug ins Different type systems for different needs, e.g.:
Aliasing/Ownership/Capability tracking Traditional types
![Page 12: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/12.jpg)
Pluggable Types in Dart?
No. Type rules are in the language spec.
Reason: worries about fragmentation, interop
Yet pluggability arose in practice, in “strong-mode”, and its subsets, which we’ll discuss later
![Page 13: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/13.jpg)
Soundishness
Dart types are unsound in at least 3 ways:
Covariant generics
Implicit downcasting on assignment
The two above interact in odd function rules
Library privacy (ADTs) vs. interface types
![Page 14: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/14.jpg)
Type Inference
Programmers want type inference
They don’t want to have to write types because they hate typing (with their fingers)
They don’t even want to read types when the types are obvious
var i = 0; // expect i to be inferred as int
![Page 15: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/15.jpg)
Type System dependent on Inference
Type Checking Type Inference
![Page 16: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/16.jpg)
Type System not dependent on Inference
Type Checking Type Inference
![Page 17: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/17.jpg)
Optional Typing Requires Smart, Integrated Tools
Checked mode control
Type checking selectively
Using metadata to disable undesired warnings
![Page 18: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/18.jpg)
Object>>hash
![Page 19: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/19.jpg)
Object>>hash maltyped
![Page 20: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/20.jpg)
Invoking the Typechecker
![Page 21: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/21.jpg)
Type Errors
![Page 22: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/22.jpg)
Type Annotations Create Expectations of Behavior
int i; // people expect i to be initialized to 0
![Page 23: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/23.jpg)
C Syntax Aggravates
Given
var i; engineers think var is a type meaning dynamic.
![Page 24: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/24.jpg)
Rational Syntax is Resisted
var i: int := 0;
Complaint is that this is too verbose, too unfamiliar
![Page 25: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/25.jpg)
Types are Knowledge
![Page 26: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/26.jpg)
Knowledge is Power
![Page 27: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/27.jpg)
Implementors Lust for Power
Especially true when classic VM technology is restricted, as when targeting the web or iOS
![Page 28: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/28.jpg)
Size is the Big Problem
Size of download on the web (more due to JS parse time than actual download)
On iOS, no JIT, so we use AOT compilation to machine code, which gets big
IOT - devices are super small
![Page 29: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/29.jpg)
Size is the Big Problem
In both web and mobile (even Android) non-native platform is at huge disadvantage; always a second-class citizen
![Page 30: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/30.jpg)
The Return of Pluggable Types?
Fully type programs prior to deployment
Check programs under sound rules
Capitalize on types in implementation
![Page 31: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/31.jpg)
The Return of Pluggable Types?
Dart’s strong mode is somewhat similar
Check programs under sound-ish rules
Some teams define their own subsets
One has to implement both behaviors :-). But really just like -Oxxx
![Page 32: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/32.jpg)
LivenessDart now allows code to be changed and reloaded without restarting
Even if your code is full type safe, the pre-existing heap and stack may not conform
If you rely on the types … Boom!
So you need a mode that does not rely on types anyway
![Page 33: Optional Typing in Dart: Purity vs. Practicepalez.github.io/STOP2016/stop2016.pdf · Dart Types Overview Nominal Interface-based Unsound for multiple reasons. Dart Types Overview](https://reader030.fdocuments.in/reader030/viewer/2022040621/5f34fd681cb6b101ce671404/html5/thumbnails/33.jpg)
Conclusion
Easier for pre-existing language; core language rules fixed, will keep you honest
Hard to retrofit into conventional design
Requires tight control over entire programming experience; not just language, but tools