Can't compare procedure pointers

Programming language questions
Post Reply
X512
Posts: 72
Joined: Sat Feb 07, 2015 2:51 pm

Can't compare procedure pointers

Post by X512 »

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?
User avatar
Ivan Denisov
Posts: 362
Joined: Tue Sep 17, 2013 12:21 am
Location: Krasnoyarsk, Russia

Re: Can't compare procedure pointers

Post by Ivan Denisov »

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.
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: Can't compare procedure pointers

Post by Josef Templ »

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
X512
Posts: 72
Joined: Sat Feb 07, 2015 2:51 pm

Re: Can't compare procedure pointers

Post by X512 »

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.
Zinn
Posts: 123
Joined: Mon Nov 24, 2014 10:47 am
Location: Frankfurt am Main
Contact:

Re: Can't compare procedure pointers

Post by Zinn »

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
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: Can't compare procedure pointers

Post by Josef Templ »

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
X512
Posts: 72
Joined: Sat Feb 07, 2015 2:51 pm

Re: Can't compare procedure pointers

Post by X512 »

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.
Post Reply