Explicitly using types
In the previous chapters, we have learned that each value has a certain data type. However, values are not the only concept in Rouge that can have data types. For instance, variables also have data types. A variable is created when you first assign a value to it. During that initial assignment, the data type of the variable is set in stone, meaning that it cannot change anymore. From that point onwards, only values of the same type can be reassigned to the variable. For instance, take a look at the following example.
The variable called age is created when the number 42 is assigned to it. The value 42 is of data type Number; therefore, the variable age is bound to type Number from that point onwards. As you can see, the first reassignment succeeds because incrementing the value stored in age still results in a Number. However, when you try to set age to a value that is not a Number, such as the value "44" (which is a String), the language will complain and refuse execution.
As you can see, a variable receives its type automatically on its first assignment. However, it is possible for you to explicitly specify the data type of a variable as well. The following example shows how you can set the type of the variable age to Number Explicitly setting a type literal in an assignment is only allowed during the initial assignment.
These spelled-out names of data types are referred to as type literals. At this point in the course, the types Number, String, and Boolean have been introduced and can be used in type literals in this exact spelling (notice they all start with a capital letter). As it has been stated previously, using type literals in assignments is entirely optional as Rouge can infer the type of variables implicitly. For this reason, it might seem like type literals are not really useful. However, at this point they serve two main purposes. First, they help you to make your code more readable by explicitly marking a variable with its type. Second, we need them for the next chapter, when we take a look at making computations reusable.