- Learn Type:Driven Development
- Yawar Amin Kamon Ayeva
- 261字
- 2021-07-02 14:41:23
Adding types
With a static type system, we can restrict our makePerson function in quite a few ways. Here's an example using ReasonML, the language that we're using in this book to learn type-driven development:
/* src/Ch01/Ch01_Demo.re */
type person = {id: int, name: string};
let makePerson(id, name) = {id, name};
Here, we define a new data type, person, and a function that creates a value of the type given the required arguments. We have one more line in the preceding code than we do in the JavaScript code, but in exchange, we get the following guarantees:
- The caller cannot pass in null or undefined arguments
- The caller cannot pass in the wrong types of arguments
- The caller cannot mutate the result value of the function
Notice in the previous example that we didn't have to declare the argument or types for the makePerson function. This is because ReasonML has great type inference that automatically understands that int, string, and person must be the only possible types allowed for those parts of the function.
ReasonML will compile the previous code into the following JavaScript:
// src/Ch01/Ch01_Demo.bs.js
function makePerson(id, name) { return [id, name]; }
As you can see, the preceding code looks almost exactly like the JavaScript we wrote earlier—the main difference is that Reason's JavaScript compiler turns records (which we'll explore later) into JavaScript arrays to take advantage of their speed.
This is just a glimpse of what static types can do to your codebase. In the coming chapters, we'll have a look at many more practical applications.
- Learning Scala Programming
- Python數據分析入門與實戰
- Arduino by Example
- Java面向對象思想與程序設計
- 羅克韋爾ControlLogix系統應用技術
- 跟老齊學Python:輕松入門
- x86匯編語言:從實模式到保護模式(第2版)
- Java性能權威指南(第2版)
- Java項目實戰精編
- 精通MATLAB(第3版)
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- Java Fundamentals
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- Swift語言實戰晉級
- HTML+CSS+JavaScript網頁制作:從入門到精通(第4版)