官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 浦江县| 观塘区| 红原县| 许昌县| 嵊泗县| 瑞丽市| 贵南县| 成都市| 芦溪县| 敖汉旗| 开原市| 恩施市| 白银市| 晋州市| 恩平市| 星子县| 新竹市| 罗甸县| 贵溪市| 农安县| 东光县| 宽城| 潼南县| 炎陵县| 专栏| 义乌市| 富顺县| 洱源县| 乐至县| 玉环县| 加查县| 察隅县| 荆州市| 鄯善县| 金乡县| 海盐县| 景泰县| 湘乡市| 溧阳市| 竹山县| 石家庄市|