Questions about compile-time techniques preventing calls on non-existing (void, also known as, null) targets in object-oriented languages. This includes usage of attachment annotations for types, problems with applying reattachment and access rules in void-safe code, issues with code patterns ensuring that particular expressions are attached to existing objects at run-time.
Background
Void-safety represents a sound static-analysis-based approach to guarantee that access on void target (causing null-pointer exceptions) may never occur at run-time in an object-oriented program that satisfies void-safety conditions. It is based on
Type system that distinguishes between attached and detachable types. Attached types apply to variables and expressions that are always known to be attached to existing objects at run-time. Detachable types apply to variables and expressions that may be Void.
Validity rules that allow for only
- safe reattachments: An expression of a detachable type may be attached only to a variable of a detachable type, while an expression of an attached type may be attached to a variable of a type with arbitrary attachment status.
- access on initialized variables: Before a variable may be accessed it has to be initialized. A variable of a detachable type may be initialized to Void (null), but a variable of an attached type has to be set to an existing or newly created object prior to its use.
Certified attachment patterns (CAP): Code patterns that guarantee attachment status of expressions, for example
... -- x may be Void or attached to an object here, we do not know. if attached x then -- x is known to be attached here, so it is safe to make a call on it: x.foo end
Resources
- Wikipedia: Void safety
- Eiffel.org: Void safety: Background
- YouTube: Introduction to Void Safety part1, part2, part3, part4
Relation to other tags
- notnull - void-safety guarantees that expressions known to be of an attached type at compile-time always yield an exiting object at run-time
- nullpointerexception, nullreferenceexception - void-safety is a compile-time guarantee of absense of these exceptions in any execution scenario