- Learn Type:Driven Development
- Yawar Amin Kamon Ayeva
- 355字
- 2021-07-02 14:41:26
Static and dynamic environments
Let's develop a mental model for what happens in a program with types and values. At its core, a program is made up of a series of type and value definitions. For example:
/* src/Ch02/Ch02_Demo.re */
type person = {id: int, name: string};
type company = {id: int, name: string, employees: list(person)};
let bob = {id: 1, name: "Bob"};
let acmeCo = {id: 1, name: "Acme Co.", employees: [bob]};
Here, we're defining person and company types, and then allocating a person (bob) and a company he works for (acmeCo).
Without worrying too much about the syntax (we will introduce this in Chapter 4, Group Values Together in Types), let's think about how the programming environment sees this program.
In a statically typed programming language, the typechecker and runtime environment together make up the static and dynamic environments. These are areas where type definitions are stored while typechecking takes place, and where value definitions are stored during program execution (runtime). We can think of these as two distinct areas that are only relevant during the distinct phases of compilation and runtime. After compilation, all type information is wiped out (type erasure), but during runtime the dynamic environment becomes active in memory (that is, the stack and the heap).
Here is how the static and dynamic environments look for the preceding code:

In each of the static and dynamic environments, each definition is allowed to refer to definitions that came before it. This is a crucial abstraction technique – it's how we build larger programs out of smaller ones at both the type and value levels.
Among other things, this strict separation balances the needs of safety and efficiency. Note that this is in sharp contrast to dynamic typing, where types exist at runtime as well, and must be checked before every operation.
- Learn to Create WordPress Themes by Building 5 Projects
- Oracle從新手到高手
- 網(wǎng)店設(shè)計(jì)看這本就夠了
- 零基礎(chǔ)學(xué)Java程序設(shè)計(jì)
- Java 11 Cookbook
- 飛槳PaddlePaddle深度學(xué)習(xí)實(shí)戰(zhàn)
- ElasticSearch Cookbook(Second Edition)
- Access 2010數(shù)據(jù)庫(kù)應(yīng)用技術(shù)實(shí)驗(yàn)指導(dǎo)與習(xí)題選解(第2版)
- Clojure Polymorphism
- Unity Android Game Development by Example Beginner's Guide
- PowerDesigner 16 從入門(mén)到精通
- LabVIEW數(shù)據(jù)采集
- Parallel Programming with Python
- Improving your Penetration Testing Skills
- 匯編語(yǔ)言程序設(shè)計(jì)