gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: proctype and procedure type checking fully implemented


From: Fischlin Andreas
Subject: Re: proctype and procedure type checking fully implemented
Date: Sun, 21 Apr 2024 13:04:00 +0000

Hi all,

I am having difficulties to follow what you try to convey. Perhaps my thoughts below help to clarify

A code such as

WriteString ('the value is: ') ; WriteCard (func, 5) ; WriteLn

as most likely intended is first of all "syntactically wrong", as it should be written as

WriteString ('the value is: ') ; WriteCard (func(), 5) ; WriteLn

If written in this manner, it should be accepted by the compiler, regardless whether the actual argument of WriteCard is a value returned by a function procedure or a variable or a constant. Perhaps we do not disagree here, do we? 

However, I find the error message far from helpful. Assignment compatibility should be referred to here and not referring to a rather vague formal and actual parameter incompatibility.

Following assignments are implicit here assuming 

VAR x: CARDINAL;
TYPE FuncType = PROCEDURE(): CARDINAL;

with a variable CARDINAL x  and procedure type FuncType

WriteCard (func, 5)  implies assignment  x := func; 
and
WriteCard (func(), 5)  implies assignment  x := func();

In the first case you assign the procedure function ‘func', not its result, to x and it becomes obvious that procedure type FuncType is not assignment compatible with CARDINAL. In the 2nd case you assign the result returned by the function ‘func’ to x and only the type of the result returned by func must be assignment compatible with x. Therefore, I would suggest to reformulate the error message to something similar to this

Type of actual parameter ‘func’ is not assignment compatible with the corresponding formal parameter of ‘WriteCard’

This would be a very generic error message that may not always help the programmer to understand what the mistake was, e.g.  WriteCard (func, 5) written but meant  WriteCard (func(), 5) and therefore some case distinction as described below may be more appropriate than above too generic error message. Your example  WriteCard (func, 5) would then lead to following compile time error message

Procedure type of ‘func’ is not assignment compatible with the corresponding formal parameter of ‘WriteCard’

Something like

PROCEDURE func () : BOOLEAN ;
BEING
...

and a statement such as  WriteCard (func(), 5) should also lead to a compilation error message. Then the compiler error message could perhaps be similar to

Type returned by ‘func’ is not assignment compatible with the corresponding formal parameter of ‘WriteCard’

to avoid using the too generic error message I mentioned first.

Then I assume we are talking only about compile time errors, right? So a function procedure such as

PROCEDURE func () : INTEGER ;
BEING
...
is accepted by the compiler with   WriteCard (func(), 5)  and leads at most to a runtime error, e.g. if func would return a negative value.

BTW, the ident of the function procedure ‘func' would profit from being written capitalized as ‘Func’. Only for a MathLib module I might tolerate lower case function procedure idents such as ’sin’, ‘cos’ as only those are established math idents. But this is irrelevant, it is just hard to read for me and against all my habits while programming in M2. ;-)

Regards,
Andreas


ETH Zurich
Prof. em. Dr. Andreas Fischlin
Systems Ecology - Institute of Biogeochemistry and Pollutant Dynamics
CHN E 24
Universitaetstrasse 16
8092 Zurich
SWITZERLAND


+41 44 633-6090 phone
+41 79 595-4050 mobile

             Make it as simple as possible, but distrust it!
________________________________________________________________________














On 20 Apr 2024, at 16:17, Gaius Mulley <gaiusmod2@gmail.com> wrote:


Hi,

a small note to say that the type checker has been improved to
fully check proctype/procedure combinations.  For example:

$ cat badproccard.mod
MODULE badproccard ;

FROM NumberIO IMPORT WriteCard ;
FROM StrIO IMPORT WriteString, WriteLn ;

PROCEDURE func () : CARDINAL ;
BEGIN
  RETURN 42
END func ;

BEGIN
  WriteString ('the value is: ') ; WriteCard (func, 5) ; WriteLn
END badproccard.

$ gm2 badproccard.mod
badproccard.mod:12:48: error: 1st parameter type failure between actual parameter type ‘func’ and the formal type ‘x’
  12 |    WriteString ('the value is: ') ; WriteCard (func, 5) ; WriteLn
     |                                                ^~~~
badproccard.mod:12:48: error: procedure type and ‘CARDINAL’ are
incompatible as formal and actual procedure parameters

regards,
Gaius


Attachment: smime.p7s
Description: S/MIME cryptographic signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]