W1102 Types

From Coder Merlin

Prerequisites[edit]

Data Types[edit]

Data types (or sometimes just types) provide a means to classify data so that the REPL or ECRD environment can properly interpret the data. A data type provides the connection between the binary format in which all data is ultimately represented in a digital computer (whether that be in main memory, a register, on disk, or in transit across a network) and the representation of that data for humans. Further, legal operations on the associated data is determined by its type. Let’s consider a few bytes in memory:

07AE: 0 1 0 0 0 0 0 1
07AF: 0 1 0 1 1 0 0 0

The byte at memory location 0x07AE might represent the integer value 65. It also might represent the ASCII (a means of encoding characters into a byte) character "A". The byte at memory location 0x07AF might represent the integer 88. It also might represent the ASCII character ‘X’. Or, perhaps, the two bytes form a single word representing a half precision floating point number. Another possibility is that the two bytes form a string beginning with the letters “AX”. Either may also be a Boolean value or part of an RGB (Red-Green-Blue) description of a pixel. Without context, the binary digits are meaningless. It is the data type in conjunction with the binary digits that provide meaning. Another way of thinking about this is that a data type constrains the set of values in the mapping from binary digits to a meaningful, human representation to a (relatively) small set of discrete elements, where each element is of the same type as all other members of the set.

Types-Set of Values.png


ComingSoonIcon.png
Coming Soon
Add section describing all basic types, including sized integers (Int8, etc.) and unsigned integers of various sizes.

Constants[edit]

A constant is a value which will not be altered as a program executes. A named constant is a constant associated with an identifier which can subsequently be used to refer to the value of the constant. In the below examples, pi, e, and g0 are all named constants:

let pi = 3.14159265358979323846
let e = 2.718281828459
let g0 = 9.80665 (in m/s2)

Named constants assist programmers to add clarity to their code. It is clearer to both the programmer herself and others that later read the code that pi refers specifically to π and not another number that happens to start with 3.14. It also helps to eliminate mistakes due to repeatedly typing or copying and pasting a constant. Further, if a mistake is discovered in a named constant it is necessary only to correct the definition of the named constant in a single location rather than attempt to search for the value throughout all of the source code. Finally, declaring named constants help to protect the programmer by enabling compile-time checks. While named constants are helpful for programmers they are also helpful to both REPL and ECRD environments as well by enabling optimizations that otherwise wouldn’t be available. Constants which are not named but rather expressed directly in the source code are called Literal Constants. For example: 145.6, “apple”, 185, -123, and false are all examples of literal constants.