1

This is one for the software archaeologists!

And before you ask why was I even bothering to try to get this to work the reason it is simply because I can - which I think is a perfectly good excuse!

I found that the following code for a procedure compiles using VAX PASCAL (and runs as expected)..

PROCEDURE format(number : INTEGER);
   VAR
      result : STRING(16);
   BEGIN
     :
     :
      writeln(result);
   END.

However if turn this into a function and try to return the result as a string it won't compile.

FUNCTION format(number : INTEGER) : STRING(16);
   VAR
      result : STRING(16);
   BEGIN
     :
     :
      format := result;
   END.

The error suggests that the error is at type definition for the function.

FUNCTION format(number : INTEGER) : STRING(16);
                                    1
PASCAL-E-TYPCNTDISCR, Type can not be discriminated in this context

I tried using VARYING and ARRAY types instead of STRING but they don't work either. Unfortunately I can't find an example of a function that returns a STRING in SYS$EXAMPLES or in the manuals I found of bitsavers.

Hopefully someone has a better memory than me.

Thanks

Mike T.
  • 165
  • 1
  • 12
  • 1
    From what I can read from docs, declare `type String16 = packed array[1..16] of char;` and define `FUNCTION format(number : INTEGER) : String16;` – LU RD Feb 19 '20 at 09:36
  • I thought about that but it just seemed too weird! I'll let you know if it works.. – Mike T. Feb 19 '20 at 10:21
  • What @LURD suggests is also what you'd need to do in Delphi (probably the most widely used Pascal these days). I think the need for the type declaration is so that the definition of the type already exists in its symbol table when the compiler encounters it in the function declaration and so that (in Delphi at least), arguments used when invoking the function can by type-checked. – MartynA Feb 19 '20 at 10:38
  • In Delphi, 'result' is a predefined variable which one can use in a function instead of the function name (as in standard Pascal). This might well be the problem here - try using a different name for the variable. – No'am Newman Feb 19 '20 at 11:12
  • "as in standard Pascal". Afaik only in FreePascal and only because it mimicked Delphi in this respect. Delphi's precursor, Turbo Pascal, did not have the Result alias, neither did Wirth's original definition of Pascal.. – MartynA Feb 19 '20 at 11:39
  • 1
    @LU RD - Weird turns out to be good! – Mike T. Feb 19 '20 at 18:47
  • (From memory) I often used `type string = varying[ 80 ] of char;` at the beginning of each VAX Pascal program, and `string` thereafter. – tonypdmtr Feb 19 '20 at 19:57

1 Answers1

2

"Pascal's type system has been described as "too strong", because the size of an array or string is part of its type, ..." Strong and weak typing

This gives a hint that the String(16) in the function return value is too vague for the compiler.

Fix that by declaring a string type that suits the compiler:

type 
  String16 = packed array[1..16] of char;

Then you can use that distinct type in the function:

FUNCTION format(number : INTEGER) : String16;
VAR
  result : String16;
BEGIN
  :
  :
  format := result;
END.

This is very much what was used in many early implementations of the pascal language (and Turbo Pascal), and is still valid. Modern compilers, like Delphi and FreePascal, has implemented a specialized dynamic array for strings, which covers a more convenient handling of the string type, not depending on declaring a strict size.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • It's like blaming C++ being a weak preprocessor for C. Oh no, that was TEN-FIFTEEN YEARS LATER than most continuously quoted Pascal criticisms. – Marco van de Voort Feb 19 '20 at 19:36