Can't compare procedure pointers

Programming language questions

Can't compare procedure pointers

Postby X512 » Sun Jun 26, 2016 7:55 am

I found that procedure pointers can't be compared if their types was declared separately:

MODULE A;
  
  TYPE
    Proc = PROCEDURE;
  
  PROCEDURE Test1 (): BOOLEAN;
    VAR
      P1, P2: PROCEDURE;
  BEGIN
    RETURN P1 = P2; (* ok *)
  END Test1;

  PROCEDURE Test2 (): BOOLEAN;
    VAR
      P1: PROCEDURE;
      P2: PROCEDURE;
  BEGIN
    RETURN P1 = P2; (* error *)
  END Test2;

  PROCEDURE Test3 (): BOOLEAN;
    VAR
      P1: Proc;
      P2: Proc;
  BEGIN
    RETURN P1 = P2; (* ok *)
  END Test3;

END A.

Is it possible to fix it?
X512
 
Posts: 37
Joined: Sat Feb 07, 2015 2:51 pm

Re: Can't compare procedure pointers

Postby Ivan Denisov » Tue Jun 28, 2016 8:15 am

Many Center members think, that it is not a bug but a feature...
http://forum.blackboxframework.org/view ... =460#p3962

So it should be some further discussion before decision to fix or not to fix.
User avatar
Ivan Denisov
 
Posts: 238
Joined: Tue Sep 17, 2013 12:21 am
Location: Krasnoyarsk, Russia

Re: Can't compare procedure pointers

Postby Josef Templ » Tue Jun 28, 2016 9:01 am

This is the intended behavior.
It gives you more control about what is compatible and what is not compatible.

A complex number, for example, could be represented using cartesian or polar coordinates.
Both have two REAL components but they are very different.
Structural equivalence would treat them as compatible, but name equivalence allows
you to make them two incompatible types.

- Josef
Josef Templ
 
Posts: 218
Joined: Tue Sep 17, 2013 6:50 am

Re: Can't compare procedure pointers

Postby X512 » Wed Jul 06, 2016 2:58 pm

I think that procedure and record are different types. In Component Pascal it is not possible to declare procedure with defined type.
Also it is possible to assign compatible procedure variables, but impossible to compare. That is inconsistent.
Code: Select all
MODULE A;
   TYPE
      Proc1 = PROCEDURE (arg1: INTEGER);
      Proc2 = PROCEDURE (arg2: INTEGER);
   
   PROCEDURE Proc (arg: INTEGER);
   BEGIN
   END Proc;
   
   PROCEDURE Do*;
      VAR P1: Proc1; P2: Proc2;
   BEGIN
      P1 := Proc; (* ok *)
      P2 := Proc; (* ok *)
      P1 := P2; (* ok *)
      ASSERT(P1 = P2); (* error *)
   END Do;
   
END A.
X512
 
Posts: 37
Joined: Sat Feb 07, 2015 2:51 pm

Re: Can't compare procedure pointers

Postby Zinn » Fri Jul 08, 2016 6:47 am

X512 wrote:I think that procedure and record are different types. In Component Pascal it is not possible to declare procedure with defined type.
Also it is possible to assign compatible procedure variables, but impossible to compare. That is inconsistent.


No, please read Josef's argument again.

change
Code: Select all
   PROCEDURE Do*;
      VAR P1: Proc1; P2: Proc2;
   BEGIN

to
Code: Select all
   PROCEDURE Do*;
      VAR P1: Proc1; P2: Proc1;
   BEGIN

and it works as you like.

- Helmut
Zinn
 
Posts: 58
Joined: Mon Nov 24, 2014 10:47 am

Re: Can't compare procedure pointers

Postby Josef Templ » Sun Jul 10, 2016 6:15 am

Comparing a procedure variable with a procedure literal is not allowed indeed.
This is a limitation that is not directly related to the question of using name or structural equivalence because in this case only structural equivalence would work.
The reasons for this limitation may be that
(1) nobody ever needed it and that
(2) it reduces the usage of structural equivalence in the compiler to the assignment of procedures to procedure variables.

What do you need this comparison for?

- Josef
Josef Templ
 
Posts: 218
Joined: Tue Sep 17, 2013 6:50 am

Re: Can't compare procedure pointers

Postby X512 » Mon Jul 11, 2016 4:16 pm

I used procedure pointer comparison for registering and unregistering handlers defined by procedures, so handler can't be registered twice and handler can be unregistered by it's procedure pointer. This is used for "text as interface" pattern, handlers are defined by Module.Procedure in text. Currently problem is solved by defining procedure type for handler, but this is a bit annoying. Anyway this is not important problem.
X512
 
Posts: 37
Joined: Sat Feb 07, 2015 2:51 pm


Return to Component Pascal

Who is online

Users browsing this forum: No registered users and 0 guests

cron