Reason is that at compile time of the procedure with the open array
parameter -- assuming you compile before running a program -- you do
not know which actual parameter is going to be passed at call time of
this procedure. Since a string constant is handled as all constants in
Modula-2 as an _expression_ to be evaluated by the compiler, this
assignment statement ought to be replaced by the compiler as a true
assignment code. But which? If string variable s is of type ARRAY
[0..0] OF CHAR, then the compiler should assign only the "t" of "test",
but if at next call (within the same program) it would be of type ARRAY
[0..255] OF CHAR, then the compiler should generate a code where it
assigns all of "test" plus the 0C at the end. From this consideration
you can see that the compiler doesn't know what code to generate. In
the spirit of the principle of simplicity, i.e. to keep compilers as
small as possible, the rule is here to ask the programmer to provide
the correct algorithm, assuming this is no big deal as is indeed the
case here. Of course, in case of a string you could ask the compiler to
know that algorithm and provide it for you. But that would mean an
exception and the language would become complicated, since you can not
ask the compiler to have for any complex open array parameter, e.g.
ARRAY OF MyRecordType, such an algorithm ready. Therefore I believe the
rule is for very good reasons there and we should stick to it.|
Breeden, Thomas (tmb) wrote:
Gaius, et. al.,
Neat. I had thought you had implemented structured type returns.
I plan to avoid them like the plague :)
Though, if they discourage anyone from writing string functions that
return POINTER TO CHAR or something like that resembling that
"C" practice, they have done a good thing.
BTW, one thing that has puzzled me about the ISO Standard is that it
states "No direct assignments to open array formal parameters may be
made" - no ifs, ands, or buts.
So the, when s is an open array parameter, the
s := "test";
should be illegal in an ISO conforming compile mode
but I am not sure why.
So far as I can see, either way, if HIGH() is accurate it should be safe,
and if not, unsafe.
From: Gaius Mulley [mailto:address@hidden]
Sent: October 27, 2008 4:52 PM
To: Breeden, Thomas (tmb)
Subject: Re: [Gm2] Returning a string constant
"Breeden, Thomas (tmb)" <address@hidden> writes:
Actually, in ISO Standard Modula-2, it is legal.
now fixed in the CVS. GNU Modula-2 allows formal parameters to be
FormalParameters := '(' [ MultiFPSection ] ')' [ ':' Qualident ]
which is (after the ":") the same as PIM2 p148 line 80,
PIM3 p172 line 76, PIM4 p158 line 80 and ISO p641 C.2.4.2.
Many thanks for the bug report - it did work if a temporary
variable was used, such as:
PROCEDURE test () : string ;
t: string ;
t := "test" ;
END test ;
but it should have worked without the need for this temporary..
gm2 mailing list
Dr. Andreas Fischlin, Ph.D., Group Director
Terrestrial Systems Ecology
Institute of Integrative Biology: Ecology, Evolution, Infectious Disease
Department of Environmental Sciences, ETH Zurich
ETH Zurich, CHN E21.1
8092 Zurich, Switzerland
Phone: +41 44 633-6090 / Fax: +41 44 633-1136
_/_/_/ _/_/_/ _/ _/
_/ _/ _/ _/ Eidgenoessische Technische Hochschule Zuerich
_/_/_/ _/ _/_/_/ Swiss Federal Institute of Technology Zurich
_/ _/ _/ _/ Ecole polytechnique federale de Zurich
_/_/_/ _/ _/ _/ Politecnico federale de Zurigo
Make it as simple as possible, but distrust it!