-
Notifications
You must be signed in to change notification settings - Fork 30
Description
Summary
The Pure aspect can currently applied to packages denoting that the package does not carry internal state. This RFC aims to introduce the same aspect for functions to not have any side effects.
Motivation
In Ada function and procedure calls that do not take arguments are done without appending and empty set of parentheses (e.g. proc; instead of proc();). This RFC is written with the context that this property of the language might change and that calls without arguments require an empty set of parentheses. The following part of the RFC assumes that this change has been applied to the language.
The ability to call functions that do not take arguments without parentheses has a specific degree of flexibility especially useful in cases where multiple implementations exist for the same interface. For example a program could want to access a value that needs to be computed on some systems and is static on others:
with Unit;
...
procedure P is
begin
...
Value := Unit.Value;
...
end P;The specification and implementation of Unit can decide whether a function, a constant or a package level variable is the best way to provide Value depending on its environment. This is in particular important in an embedded environment where resource usage is important and abstractions are commonly handled by selecting different sets of sources to implement a common interface.
To address this problem this RFC proposes to introduce the aspect Pure for function declarations:
function F return Integer with
Pure;The Pure aspect allows this function to be called without parentheses. It also disallows it from having side effects, such as writing to a variable outside the functions local scope. The latter property exists to ensure a similar behavior between an access to a constant and the function call.
As a reference a similar concept exists already in Python which allows a class method to have the @property decorator. It also allows objects to implement a property directly through a variable, or alternatively with a function that does not take arguments. The difference is that @property does not imply the absence of side effects and functions with this decorator are free to do anything the language allows.
Caveats and alternatives
The aspect name Pure was chosen because it already exists in a similar context. Having the behavior of Pure consistent is one of the reasons to enforce the absence of side effects. Alternatively side effects could be allowed and the aspect named differently.
While the absence of side effects on a language level may not be violated in some cases a call to a pure function may still have side effects, e.g. through an imported call to a C library.
This interface is most useful when the caller already expects to have a constant. In this case it is possible to implement this access through a function. If the caller expects to call a function a simple function returning a constant does not incur a runtime overhead if it is optimized by the compiler.