The basic idea behind references is pretty simple. Instead of having a variable hold a value, we want the variable to hold a reference to some value. A reference is effectively an address to the memory location where the value has been stored. Using this reference, we can access or change the value the reference points to.

Let's illustrate this with a simple example. The & operator obtains a reference to a variable, and therefore to the value it holds. The * operator can then be applied to the reference to get or change the value pointed to by the reference:

mut a = 3
imm ref = &a    // ref holds a reference to a's contents
imm val = *ref  // val is now 3
*ref = 4        // a is now set to 4

Benefits and Safety Risks

References offer important benefits:

However, without appropriate constraints, the unfettered ability to access and change any arbitrary value based on its address opens up a program to significant safety risks:

Such failures are not always easy to detect and eliminate when the responsibility for being careful lies 100% with fallible humans. Although the consequences are not always serious, they could be catastrophic, especially when malicious actors take advantage of undetected pointer problems to gain access or control over other peoples' privileges or information.

Because of these safety risks, Cone explicitly distinguishes between references and pointers. Although both offer addressable access to values, references are constrained to prevent unsafe use. Pointers are not subject to such constraints, making them more versatile but also potentially more dangerous.

Reference Mechanisms and Flavors

The way that Cone handles references is unusual among programming languages. Although many capabilities are similar to those found in other languages, Cone's distinctive approach optimizes for versatility, safety and ease-of-use.

Getting comfortable with Cone's references begins with understanding the four fundamental mechanisms that underlie their capabilities and constraints.

In addition to basic references, Cone also supports more complex reference types that carry more than a pointer:

The default condition for references is that they can only point to valid values. However, it is possible to also declare and create nullable references. A nullable reference can have the value null, which means the reference does not point to any valid value. Protective measures ensure that one can never dereference a nullable reference whose value is null.

_