Discussion:
What is "ARRAY 0 OF something" useful for in Oberon-07
(too old to reply)
Guy T.
2020-03-21 19:27:29 UTC
Permalink
Hello again,

The Project Oberon compiler allows for a definition as follow:

VAR a: ARRAY 0 OF CHAR;

Is there a useful case for defining a zero length array? I can't see any.

Thanks!

Guy
c***@gmail.com
2020-03-22 08:37:12 UTC
Permalink
Post by Guy T.
Hello again,
VAR a: ARRAY 0 OF CHAR;
Is there a useful case for defining a zero length array? I can't see any.
I don't recall ever making use of it myself but I can think of a possible scenario where it could be useful:

Oberon does not allow optional parameters to procedures. Just suppose you wanted to implement a procedure that generally took an array as a parameter but there were situations where it was not needed. If you could not create a zero array it would not be quite as clear to the reader that it is to be ignored. e.g. something like this:

VAR
null: ARRAY 0 OF INTEGER;
values: ARRAY 10 OF INTEGER;
i: INTEGER;

PROCEDURE P(i: INTEGER; a: ARRAY OF INTEGER);
BEGIN
IF LEN(a) > 0 THEN ...
END P;

BEGIN
P(i, null);
P(i, values);
...
...

P.S. I'm not necessarily recommending this practice ;-)

Cheers,
Chris
August Karlstrom
2020-03-22 14:01:49 UTC
Permalink
Post by c***@gmail.com
P.S. I'm not necessarily recommending this practice ;-)
To my mind the following sentence from the language report suggests that
an array can not have length zero:

"The elements of the array are designated by indices, which are integers
between 0 and the length minus 1."

https://www.miasap.se/obnc/oberon-report.html


-- August

Continue reading on narkive:
Loading...