[fpc-devel] Could FPC add the PLM "based" construct?

Giuliano Colla giuliano.colla at fastwebnet.it
Mon Nov 16 12:12:56 CET 2009


Hello FPC developers

I've been using Algol-like languages since the times of Algol '60.
In general I'm rather happy with FPC, but I miss a feature which I found 
in Intel's PLM languages. I'd like to submit it to see if there's a 
chance it could be introduced in FPC.

The feature is the *based* construct, which makes IMHO typed pointers 
much easier to deal with. It makes the code easier to write, more 
readable, and shouldn't break anything existing: it would be a language 
extension, which, in my incompetent judgment, should be fairly simple to 
implement. Those who don't like it could just avoid using it, and 
continue the old way.

For those unfamiliar with PLM, the construct in FPC could like that:

var
   Pfoo: pointer;
   foo: "any valid FPC Type" based Pfoo;
or
   foo: based Pfoo "any valid FPC type";
.....
   Pfoo := APointer;
   foo := Whatever;
   WhateverElse := foo;
	

This would make Pfoo an untyped pointer, while any occurrence of foo 
would be replaced by the compiler with a Pfoo^, typed unambiguously by 
foo type.

Multiple declarations are allowed such as:

var
   Pfoo: pointer;
   Bfoo: byte based Pfoo;
   Ifoo: Integer based Pfoo;
....
implementation
....
   Pfoo := ABytePointer;
   Bfoo := WhateverByte;
....
   Pfoo := AnIntegerPointer;
   WhateverInteger := Ifoo;


This becomes particularly handy when the fpc types more complex, such as 
different record types.

Currently, to achieve the same result you need to write more, without 
adding to readability and maintainability, but perhaps adding something 
to obscurity instead:

type
   PByte: ^byte;
   PInteger: ^Integer;
...
var
   Pfoo: pointer;
   PBfoo: PByte absolute Pfoo;
   PIfoo: PInteger absolute Pfoo;
....
implementation
....
   PBfoo := ABytePointer;
   PBfoo^ := whateverByte;
...
   PIfoo := AnIntegerPointer;
   whateverInteger := PIfoo^;

Something of the sort is already done with objects or strings where the 
pointer declaration is implicit. This construct would extend the same 
logic to all other types, but with an explicit declaration of the pointer.

It would, IMHO help to get rid of some of the obscurity inherited by the 
C language constructs.
This would also help to get rid, of the ambiguities (also inherited for 
C) where a variable name sometimes means the value, sometimes means the 
pointer, depending on context.

Any opinion?

-- 
Giuliano Colla

Whenever people agree with me, I always feel I must be wrong (O. Wilde)



More information about the fpc-devel mailing list