Void safety

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Void safety (also known as null safety) is a guarantee within an object-oriented programming language that no object references will have null or void values.

In object-oriented languages, access to objects is achieved through references (or, equivalently, pointers). 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 no void calls will ever arise.


In a 2009 talk, Tony Hoare traced the invention of the null pointer to his design of the Algol W language and called it a "mistake":

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.[1]

Bertrand Meyer introduced the term "void safety".[2]

In programming languages[edit]

An early attempt to guarantee void safety was the design of the Self programming language.

The Eiffel language is void-safe according to its ISO-ECMA standard; the void-safety mechanism is implemented in EiffelStudio starting with version 6.1 and using a modern syntax starting with 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.[3]

The C# language implements compile-time null safety check since version 8. However, to stay compatible with anterior versions of the language, the feature is opt-in on a per project or per file basis.

The Kotlin language, a JVM language, and Apple's Swift language use null-safe types by default.

See also[edit]


  1. ^ C.A.R. Hoare: Null References: The Billion Dollar Mistake, abstract of a talk at QCon London, 9–12 March 2009, available online
  2. ^ Bertrand Meyer: Attached Types and their Application to Three Open Problems of Object-Oriented Programming, in ECOOP 2005 (Proceedings of European Conference on Object-Oriented Programming, Edinburgh, 25–29 July 2005), ed. Andrew Black, Lecture Notes in Computer Science 3586, Springer Verlag, 2005, pages 1-32, also available online
  3. ^ Syme, Don. "Quote of the Week: "What can C# do that F# cannot?"". Retrieved 7 September 2018.