This program illustrates the use of unsafe constructs, such as
LOOPHOLE
, an unsafe cast. The default mode for
Modula-3 programs is SAFE
, i.e., the language runtime
is responsible for checking run-time errors. For programming
intricate systems, integrating legacy systems,
or making your programs more efficent, you may decide that
you would like the freedom to perform tasks that circumvent
the safety conditions enforced by the language run-time.
Modula-3 gives you the freedom to do unsafe operations in
in the UNSAFE
modules by providing more operations,
such as LOOPHOLE
(an unsafe cast) or ADR
(returns
the address of a variable) which do not work well with
safe programming practices enforced in SAFE
modules.
With this freedom for you the programmer, comes the
responsibility to check for run-time errors. The language
does not check for type safety in a LOOPHOLE
.
The separation of safe and unsafe codes is essential for writing
portable programs that utilize unsafe or non-portable features
of particular systems. Indeed it is common practice for
systems progammers to divide up their code in safe and
unsafe portions. This way, the bulk of porting to
a new platform, lies in the unsafe portion. Modula-3 supports
this programming model. Both interfaces and modules can be
marked as UNSAFE
. You are best to code most (if not all)
of your programs in the safe mode, since it is much easier
to make safe programs robust.
A safe module can only
import safe interfaces, so in safe programming you can't mistakenly
count on unsafe functionality in another unsafe module. An unsafe module
can make its functionality available to other safe modules
by exporting a SAFE
interface.
One nice aspect of the support for unsafe features is that you don't have to rely on external calls, or implementation-specific features to make your programs more efficient. Indeed, the unsafe portions of your code will have as much control over the representation an layout of your data structures as you have when programming in an unsafe language like C. Indeed people have gone as far as writing operating system in Modula-3; something that is not possible in other high-level languages of its class.