|Subject:||Re: [Gm2] Use of functions in expressions|
|Date:||Sun, 16 Nov 2008 16:12:10 +0100|
|User-agent:||Thunderbird 188.8.131.52 (Macintosh/20080914)|
Exactly because of such rather messy code did Wirth in his original language defintion NOT allow for such constructs. That some compilers accept such code is actually only due to the ISO standard of Modula-2. Prior to this your first formulation was not legal either and since I am not an adept with the ISO standard, I have no strong opinions about the pros and cons of this ISO extension of the original Modula-2. I'm wary it might complicate matters unnecessarily, something fundamentally at odds with Modual-2. Remember also that the ISO standard is not without difficulties, which makes people like me rather sceptical with respect to its benefits, except for settling on a standard library (but that has little to do with the language itself). In this context see also 10 little Modulans... http://www.scifac.ru.ac.za/cspt/sc22wg13.htm#Ten .
First, please note that I see very little reason to write in Modula-2 such code as you wrote. Function procedures should be used only if they return a well defined object, e.g. a real variable, if they have no side effects and if they can be used in place of a variable in an _expression_ (e.g. think of it as a factor http://cui.unige.ch/db-research/Enseignement/analyseinfo/Modula2/factor.html). Therefore my first question to you whould be: Why do you want to have string expressions? Unless you would have some applications where you really want to multiply a string meaningfully with a number - or something similarly exotic - I see little sense in that. Modula-2 is helping you to stay away from such applications. Conclusion: use a procedure, NOT a function procedure.
Second, when you comment some code by "this works", "this does not work" then it is not clear what you mean from that. You should in general always distinguish between, code that can be compiled, because the compiler accepts it as legal, or whether the code does fail during execution, typically causing a run-time error. From what you write further down I guess that you mean "can be compiled" vs. "can't be compiled".
Thirdly, Write(tester()); is a phantasy code clearly in conflict with any Modula-2 syntax I know of. It conflicts with the BNF for a procedure call (see http://cui.unige.ch/db-research/Enseignement/analyseinfo/Modula2/procedure_call.html). Therefore any Modula-2 compiler should reject it as syntactically wrong.
A correct solution to what you try to do would be:
PROCEDURE Tester(VAR s: ARRAY OF CHAR);
That is all not very complicated and IMHO quite elegant code. Why trying something different that gives you and compiler builders only trouble?
I recommend you give up on your idea of using a function procedure in relation with strings as illustrated above. Of course, there are some rather rare cases in which it may make sense to work with function procedures with respect to strings. E.g. if such a function returns an opaque string object or a single CHAR. See this module from the 'Dialog Machine' for such procedures:
http://se-server.ethz.ch/RAMSES/Objects/DM/DMStrings.html#DMStrings (with Comments)
http://se-server.ethz.ch/RAMSES/Objects/DM/DM_Quick_Reference.html#DMStrings (without Comments)
The case of a single CHAR may be interesting in a conversion function such as
PROCEDURE Upper(ch: CHAR): CHAR;
RETURN CAP(ch) (* should of course be something more meaningful *)
and its application
PROCEDURE StringToUpper(VAR s: ARRAY OF CHAR);
VAR i,n: INTEGER;
n := HIGH(s); WHILE (i<=n) AND (s[i]<>0C) DO s[i] := Upper(s[i]); INC(i) END;
all not very useful, but I hope illustrative.
In general, please note also, Modula-2 is NOT C and arrays, including strings, should be considered what they are, a structured data type that has first of all nothing to do with dynamic data as passed by function procedures via the stack or pointers referencing data in the heap. An array is a static data structure with a finite size allocated by the compiler at compile time. It faciliates repetitive operation on its element, which, BTW, can be very complex, i.e. it can be an entire RECORD of a very complex type.
Hope this helps to solve your problems.
Scott Robinson wrote:
________________________________________________________________________ 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! ________________________________________________________________________
|[Prev in Thread]||Current Thread||[Next in Thread]|