GNU Pascal Homepage - gpc - gpc-announce - gpc-de - gpc-doc
Diese Seite auf deutsch

Mail #12380

Back to main page of archive

Previous mail   Next mail   Unformatted/full headers
Overview  10 days   Subject   Date   Thread   Author  

From: Gale Paeper
Subject: Forward defined types (and object
Date: 7 Jul 2005, 19:27:24

Frank Heckenbach wrote:
> 
> Gale Paeper wrote:
> 
[snip]
> > The forward object type declaration must have a complete type
> > declaration in the same type declaration part (like Pascal's requirement
> > for pointer types).  The commented out var declaration part and type
> > keyword is what I used to test that requirement. Uncomment that part and
> > you get:
> >
> > Error   : unresolved forward class reference to 'objA'
> > ForwardObjectTest.p line 11   var
> 
> Good (in accordance with OOE, except for the syntax).

The other part of OOE's deferred class requirement (i.e., "cannot be
used in a class-inheritance-list before its complete definition is
specified") is also enforced.  A slight modification of the test program
I've been using for an example:

program ForwardObjectTest1;

type	
  objA = object; forward;
  objB = object
    {...}
    o : objA; { objB uses objA }
    {...}
  end;

  { WRONG - Can't inherit from a FORWARD declared object }
{ objC = object(objA)
    
  end;  
}  
  objA = object
    {...}
    p : objB; { objB uses objA }
    {...}
  end;  

  { OK - Can inherit from a FORWARD object after it is fully defined }
  objD = object(objA)
    { objD inherits from objA }
  end;  
     
begin
end.

With the objC type declaration commented out the program compiles
without error.  Uncomment the objC type declaration and you get the
follow compilation error with CodeWarrior Pascal:

Error   : class 'objA' was declared 'FORWARD' or 'EXTERNAL'
ForwardObjectTest1.p line 12   objC = object(objA)

For the sake of completeness and since the error message refers to
external object declarations, I suppose CodeWarrior Pascal and MPW
Pascal's support for external object declarations should be mentioned.

The basic purpose and reason for external object declaration is the same
as it is for forward declared objects except it is used for mutually
dependent object type declarations between unit interfaces.

Before an example program demonstrating the external object feature and
how it can be used, a few notes on this particular MacPascal Object
Pascal feature:

1.  I'm not sure any real world program actually makes use of it.

2.  Even Apple, who invented the feature for MPW Pascal, called code
using external objects "ugly" and the code, even though it maybe useful, confusing.

3.  It isn't the easiest feature to make use of.  It takes some thought
and care in organizing code to avoid circular unit interface dependency
errors and unresolved external object declaration errors.

So, there's no misunderstanding, I'm not asking for GPC's support for
MacPascal style objects to include external object declaration support. 
Put it in the "to be considered if someone really needs it" bin.  I'm
covering MacPascal external object declaration support so that Frank and
Waldek are at least aware of the feature and have an example of its usage.

The example program and units are:

program ExternalObjectTest;

uses UObjA, UObjB;
	
var
  ObjAInstance: ObjA;
  ObjBInstance: ObjB;

begin
	
	ObjAInstance := NewObjA;
	ObjBInstance := NewObjB;
	ObjAInstance.InitwithObjB(ObjBInstance);
	ObjBInstance.InitwithObjA(ObjAInstance);
	ObjAInstance.TestObjAsObjBReference;
	ObjBInstance.TestObjBsObjAReference;
	Dispose(ObjAInstance);
	Dispose(ObjBInstance);
end.

unit UObjA;

interface

type
  ObjB = object; external;
  ObjA = object
    fObjB: ObjB;
    procedure InitwithObjB(InitObj: ObjB);
    procedure TestObjAsObjBReference;
    procedure TestIt;
  end;
  
function NewObjA: ObjA; 

implementation

  uses UObjB;
  
  function NewObjA: ObjA;
  var
    obj: ObjA;
  begin
    new(obj);
    NewObjA := obj;
  end;
  
  procedure ObjA.InitwithObjB(InitObj: ObjB);
  begin
    fObjB := InitObj;
  end;

  procedure ObjA.TestObjAsObjBReference;
  begin
    writeln ('In ObjA calling fObjB');
    fObjB.TestIt;
  end;
  
  procedure ObjA.TestIt;
  begin
    writeln ('In ObjA TestIt');
  end;
  
end.

unit UObjB;

interface

  uses UObjA;
		
type
  ObjA = object; external;
  ObjB = object
    fObjA: ObjA;
    procedure InitwithObjA(InitObj: ObjA);
    procedure TestObjBsObjAReference;
    procedure TestIt;
  end;
 
function NewObjB: ObjB; 

implementation

  function NewObjB: ObjB;
  var
    obj: ObjB;
  begin
    new(obj);
    NewObjB := obj;
  end;

  procedure ObjB.InitwithObjA(InitObj: ObjA);
  begin
    fObjA := InitObj;
  end;

  procedure ObjB.TestObjBsObjAReference;
  begin
    writeln ('In ObjB calling fObjA');
    fObjA.TestIt;
  end;
  
  procedure ObjB.TestIt;
  begin
    writeln ('In ObjB TestIt');
  end;
  
end.

When compiled with CodeWarrior Pascal and subsequently ran, the program
output is:

In ObjA calling fObjB
In ObjB TestIt
In ObjB calling fObjA
In ObjA TestIt

Gale Paeper
gpaeper@empirenet.com

Previous mail   Next mail   Unformatted/full headers
Overview  10 days   Subject   Date   Thread   Author  


Replies

Author Subject Date
Frank Heckenbach Forward defined types (and object 8 Jul 2005, 12:01:27

In reply to

Author Subject Date
Waldek Hebisch Forward defined types (and object references) 6 Jul 2005, 20:41:10
Gale Paeper Forward defined types (and object references) 7 Jul 2005, 00:36:32
Frank Heckenbach Forward defined types (and object 7 Jul 2005, 12:50:57

Back to main page of archive


Note: This page contains information that does not originate from the owner of this web site, but from the authors of the mails archived. The owner of this web site is not responsible for the content of such information. Any use of that infomation requires the consent of the respective author.

Where WWW addresses (URLs) in the mails archived are marked as hyperlinks, this is only for the comfort of the reader. The content of the web pages linked to like this does not necessarily reflect the opinion of the owner of this web site or of the authors of the mails archived. The owner of this web site is not responsible for the content of such web pages. Those pages are explicitly not to be considered as part of the content of this page, but merely as references.


This page was created by Crystal 0.999 (Linux 2.4.27/i686).