Null Safety
   HOME

TheInfoList



OR:

Void safety (also known as null safety) is a guarantee within an
object-oriented programming language Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impleme ...
that no object references will have ''null'' or ''void'' values. In object-oriented languages, access to objects is achieved through
references A reference is a relationship between Object (philosophy), objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. ...
(or, equivalently,
pointers Pointer may refer to: People with the name * Pointer (surname), a surname (including a list of people with the name) * Pointer Williams (born 1974), American former basketball player Arts, entertainment, and media * ''Pointer'' (journal), the ...
). A typical call is of the form: x.f(a, ...) where ''f'' denotes an operation and ''x'' denotes a reference to some object. At execution time, however, a reference can be ''void'' (or ''null''). In such cases, the call above will be a void call, leading to a run-time exception, often resulting in abnormal termination of the program. Void safety is a static (compile-time) guarantee that a void call will never arise.


History

In a 2009 talk,
Tony Hoare Sir Charles Antony Richard Hoare (; born 11 January 1934), also known as C. A. R. Hoare, is a British computer scientist who has made foundational contributions to programming languages, algorithms, operating systems, formal verification, and ...
traced the invention of the
null pointer In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value saved for indicating that the Pointer (computer programming), pointer or reference (computer science), reference does not refer to a valid Object (c ...
to his design of the
ALGOL W ALGOL W is a programming language. It is based on a proposal for ALGOL X by Niklaus Wirth and Tony Hoare as a successor to ALGOL 60. ALGOL W is a relatively simple upgrade of the original ALGOL 60, adding string, bitstring, complex number a ...
language and called it a "mistake":
Bertrand Meyer Bertrand Meyer (; ; born 21 November 1950) is a French academic, author, and consultant in the field of computer languages. He created the Eiffel programming language and the concept of design by contract. Education and academic career Meyer ...
introduced the term "void safety".


In programming languages

An early attempt to guarantee void safety was the design of the Self programming language. The
Eiffel language Eiffel is an object-oriented programming language designed by Bertrand Meyer (an object-orientation proponent and author of ''Object-Oriented Software Construction'') and Eiffel Software. Meyer conceived the language in 1985 with the goal of incr ...
is void-safe according to its
ISO The International Organization for Standardization (ISO ; ; ) is an independent, non-governmental, international standard development organization composed of representatives from the national standards organizations of member countries. Me ...
- ECMA standard; the void-safety mechanism is implemented in EiffelStudio starting wit
version 6.1
and using a modern syntax starting wit
version 6.4
The Spec# language, a research language from Microsoft Research, has a notion of "non-nullable type" addressing void safety. The F# language, a functional-first language from Microsoft Research running on .NET framework, is void-safe except when interoperating with other .NET languages.


Null safety based in union types

Since 2011 several languages support
union type Union commonly refers to: * Trade union, an organization of workers * Union (set theory), in mathematics, a fundamental operation on sets Union may also refer to: Arts and entertainment Music * Union (band), an American rock group ** ''Unio ...
s and intersection types, which can be used to detect possible null pointers at compiling time, using a special class Null of which the value null is its unique instance. The null safety based in types appeared first in
Ceylon Sri Lanka, officially the Democratic Socialist Republic of Sri Lanka, also known historically as Ceylon, is an island country in South Asia. It lies in the Indian Ocean, southwest of the Bay of Bengal, separated from the Indian subcontinent, ...
, followed soon by
TypeScript TypeScript (abbreviated as TS) is a high-level programming language that adds static typing with optional type annotations to JavaScript. It is designed for developing large applications and transpiles to JavaScript. It is developed by Micr ...
. The C# language implements compile-time null safety check since version 8. However, to stay compatible with older versions of the language, the feature is opt-in on a per project or per file basis. The Google's Dart language implements it since its version 2.0, in August 2018 Other languages that use null-safe types by default include JetBrains' Kotlin,
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
, and Apple's
Swift Swift or SWIFT most commonly refers to: * SWIFT, an international organization facilitating transactions between banks ** SWIFT code * Swift (programming language) * Swift (bird), a family of birds It may also refer to: Organizations * SWIF ...
.


See also

*
Nullable type Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type. In statically typed languages, a nullable type is an option type, while i ...
*
Option type Option or Options may refer to: Computing * Option key, a key on Apple computer keyboards * Option type, a polymorphic data type in programming languages * Command-line option, an optional parameter to a command *OPTIONS, an HTTP request metho ...
*
Safe navigation operator In object-oriented programming, the safe navigation operator (also known as optional chaining operator, safe call operator, null-conditional operator, null-propagation operator) is a binary operator that returns null if its first argument is null; ...


References

Object-oriented programming {{Comp-sci-stub