[Oberon] CONST parameters
Chris Burrows
chris at cfbsoftware.com
Sat Jun 17 07:22:57 CEST 2017
> -----Original Message-----
> From: Oberon [mailto:oberon-bounces at lists.inf.ethz.ch] On Behalf Of
> peter at easthope.ca
> Sent: Wednesday, 14 June 2017 2:25 PM
> To: oberon at lists.inf.ethz.ch
> Subject: Re: [Oberon] Text buffer analogue of Strings.Prefix.
>
> From: Peter Matthias <PeterMatthias at web.de>
> Date: Tue, 13 Jun 2017 21:55:53 +0200
> > Which version of PC Native O. has such CONST declaration?
>
> Will check the alpha release again tomorrow and might need to correct
> my statement.
>
> LinuxAos (rev.6661) certainly has it. Still puzzled that a parameter
> is constant.
>
Maybe you are thinking that CONST parameters are like CONST declarations.
i.e. that the parameter always has the same value? That is not so. Constant
parameters allow any different value to be passed to the procedure via the
parameter whenever the procedure is called. The CONST keyword protects the
parameter from being changed within the body of the procedure and any
procedures that it calls i.e. the value is read-only. This feature helps
programmers to improve the security, reliability, testability and
maintainability of their programs.
In other languages (e.g. Ada, Component Pascal etc.) they are declared as
'IN' parameters (which might be less confusing).
A long-standing problem in Pascal, Modula-2, Oberon and Oberon-2 is that if
you wanted to protect an ARRAY parameter from being accidentally modified by
a procedure you would omit the VAR keyword when declaring the parameter. The
system would then make a copy of the array every time you called the
procedure. If the array is large the time taken to make the copy could be a
significant overhead. Hence, programmers have got into the bad habit of
declaring these arrays as VAR just to avoid performance problems. The
tradeoff was the increased probability of accidental changes to the array
occurring, requiring more extensive testing to be done.
In Oberon-07 ARRAYs that not declared as VAR parameters are enforced as
read-only parameters at compile time. No copy of the array is required. This
is the default behaviour - no CONST or IN keyword is required. This means
that Oberon programmers no longer have to take the risk of declaring large
ARRAYs as VAR parameters just to avoid performance problems.
Regards,
Chris Burrows
CFB Software
http://www.astrobe.com
Oberon for ARM Cortex-M3, M4 and M7 microcontrollers
More information about the Oberon
mailing list