- Learn Type:Driven Development
- Yawar Amin Kamon Ayeva
- 354字
- 2021-07-02 14:41:26
Values
It's important to understand how values work in Reason. We've seen that they come into play at runtime and live in memory but it's also important to know that, by default, all values are immutable –effectively, constants. There are a couple of exceptions, which we will cover, but generally we will work in a style where we don't try to change values, and instead just create new values out of old ones. This is a style that is well-supported by Reason and is a foundation of functional programming.
There is a syntax for binding values to names, which is as follows:
let PATTERN = VALUE;
The preceding syntax slots the value on the right-hand side into the shape described in the left-hand side, as long as their shapes match. The general name for this concept is pattern matching, and we will see it a lot in this book.
So far, the patterns (to the left of =) we've seen have been just simple names, such as:
let x = 1;
The preceding pattern has allowed us to capture the entire value in the name and reuse it later. The way it works is that Reason checks that the value (1) can fit inside the pattern (x). In this simple case, there is nothing about the pattern that prevents the value from fitting inside. We call this an irrefutable pattern. In further chapters, we will see examples of refutable patterns and how they behave.
Wherever you see the keyword let, you should understand that it may be allocating memory, if:
- The bound value is a literal (for example, "Bob"), or
- The bound value is the result of a function or operator call, and the function or operator call allocates a new value in memory
The other cases are mainly of bindings to existing values or bindings to function calls that don't allocate.
In this book, we won't be worrying too much about allocation and memory use, but we will look at a couple of techniques of how to reduce them when necessary, which can come in handy when trying to boost performance.
- OpenShift開發指南(原書第2版)
- Python數據可視化:基于Bokeh的可視化繪圖
- PHP 從入門到項目實踐(超值版)
- JavaScript語言精髓與編程實踐(第3版)
- 零基礎玩轉區塊鏈
- Hands-On JavaScript High Performance
- Mastering Predictive Analytics with Python
- 從零開始:UI圖標設計與制作(第3版)
- ExtJS Web應用程序開發指南第2版
- C指針原理揭秘:基于底層實現機制
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- Mastering ASP.NET Core 2.0
- 小學生C++趣味編程從入門到精通
- 區塊鏈:技術與場景
- 軟件測試項目實戰之功能測試篇