[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Gm2] Returning a string constant
From: |
Iztok Kobal |
Subject: |
Re: [Gm2] Returning a string constant |
Date: |
Mon, 27 Oct 2008 07:53:52 +0100 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.17) Gecko/20080922 SUSE/1.1.12-1.1 SeaMonkey/1.1.12 |
MODULE test1;
FROM InOut IMPORT WriteString, WriteLn;
FROM Strings IMPORT Assign;
TYPE
testresult = ARRAY [0..9] OF CHAR;
(* physical allocation of return value container is needed !!! *)
VAR
s : testresult;
(* Now it is correct ;-) *)
PROCEDURE test(VAR s: ARRAY OF CHAR);
BEGIN
(* s := "test"; may be accepted by some compilers, but is not
recommended, since not safe. It is better to use Strings.Assign *)
Assign("test",s);
(* there is no need to have an explicit RETURN statement here *)
END test;
BEGIN
test(s);
WriteString('Test results = "');
WriteString(s);
WriteString('"');
WriteLn;
END test1.
Regards, Iztok
Andreas Fischlin wrote:
> Dear Scott,
>
> In standard Modula-2 yes, this is illegal. Wirth (1983) writes on p.
> 52: "Only one value, however, can be returned as the result of a
> function. This value, moreover, cannot be of a structured type."
>
> Please consider the following three arguments: (i) Functional
> procedures may be used in expressions, and using a string in an
> expression doesn't make much sense. (ii) You have plenty of
> alternatives: Simply use procedure parameters and there is no need to
> return an object such as a string from a functional procedure (see
> alternative code provided below). (iii) Code does not look very good
> and it is a bad habit from C/Unix programmers to always use functional
> procedures and then to return the actual results as a side effect from
> the call to the procedure. In a easy to read programing style
> returning complex data from a procedure is best documented by using a
> procedure that is not a functional procedure and returns all the data
> through procedure parameters.
>
> Considering this your example can be rewritten as:
>
> MODULE test1;
>
> FROM InOut IMPORT WriteString, WriteLn;
> FROM Strings IMPORT Assign;
>
> TYPE
> testresult = ARRAY [0..9] OF CHAR;
>
> PROCEDURE test(VAR s: ARRAY OF CHAR);
> BEGIN
> (* s := "test"; may be accepted by some compilers, but is not
> recommended, since not safe. It is better to use Strings.Assign *)
> Assign("test",s);
> (* there is no need to have an explicit RETURN statement here *)
> END test;
>
> BEGIN
> test(testresult);
> WriteString('Test results = "');
> WriteString(testresult);
> WriteString('"');
> WriteLn;
> END test1.
>
> Hope this helps and answers your question.
>
> Regards,
> Andreas
>
> Cited references:
> --------------------
> Wirth, N., 1983 (2 (corr.) ed.). Programming in Modula-2.
> Springer-Verlag: Berlin a.o, 176 pp.
>
>
> Scott Robinson wrote:
>> Hello all,
>>
>> I was wondering if the Modula-2 code below is supposed to be illegal?
>>
>> MODULE test1;
>>
>> FROM InOut IMPORT WriteString, WriteLn;
>>
>> TYPE
>> testresult = ARRAY [0..9] OF CHAR;
>>
>> PROCEDURE test() : testresult;
>> BEGIN
>> (* Is this legal? *)
>> RETURN "test";
>> END test;
>>
>> BEGIN
>> WriteString('Test results = "');
>> WriteString(test());
>> WriteString('"');
>> WriteLn;
>> END test1.
>>
>> The Modula-2 compiler is giving me this:
>>
>> #gm2 -c -g test1.mod
>> test1.mod: In function ‘test’:
>> test1.mod:11: error: conversion to non-scalar type requested
>>
>> Thanks,
>> Scott
>>
>>
>>
>> _______________________________________________
>> gm2 mailing list
>> address@hidden
>> http://lists.nongnu.org/mailman/listinfo/gm2
>
> --
> ________________________________________________________________________
> Dr. Andreas Fischlin, Ph.D., Group Director
>
> Terrestrial Systems Ecology
> Institute of Integrative Biology: Ecology, Evolution, Infectious Disease
> Department of Environmental Sciences, ETH Zurich
>
> Address:
> ETH Zurich, CHN E21.1
> 8092 Zurich, Switzerland
>
> Phone: +41 44 633-6090 / Fax: +41 44 633-1136
> http://www.sysecol.ethz.ch/Staff/af/
> http://www.sysecol.ethz.ch/
>
> _/_/_/ _/_/_/ _/ _/
> _/ _/ _/ _/ 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!
> ________________________________________________________________________
>
> * *
> ------------------------------------------------------------------------
>
> _______________________________________________
> gm2 mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/gm2
>
- [Gm2] Returning a string constant, Scott Robinson, 2008/10/26
- Re: [Gm2] Returning a string constant, Andreas Fischlin, 2008/10/26
- Re: [Gm2] Returning a string constant,
Iztok Kobal <=
- RE: [Gm2] Returning a string constant, Breeden, Thomas (tmb), 2008/10/27
- Re: [Gm2] Returning a string constant, Gaius Mulley, 2008/10/27
- RE: [Gm2] Returning a string constant, Breeden, Thomas (tmb), 2008/10/27
- Re: [Gm2] Returning a string constant, Gaius Mulley, 2008/10/27
- Re: [Gm2] Returning a string constant, Andreas Fischlin, 2008/10/28
- Re: [Gm2] Returning a string constant, Iztok Kobal, 2008/10/30
- Re: [Gm2] Returning a string constant, Christian Maurer, 2008/10/28