[fpc-pascal] Re: String type compatibility

Jonas Maebe jonas.maebe at elis.ugent.be
Wed Apr 3 16:43:46 CEST 2013


On 03 Apr 2013, at 16:25, Xiangrong Fang wrote:

>> It's because in Pascal you need to declare types first.
>
> But why array[0..10] of Integer, or string[255] are not "types"?

They are type definitions. Parameter lists cannot contain type  
definitions, only previously defined types. The main reason is that  
defining the same structured type multiple types results in different  
types. E.g.:

***
unit tt7;

interface

type
   ta = array[0..10] of Integer;
   tb = array[0..10] of Integer;

procedure test(a: ta);
procedure test(b: tb);

implementation

procedure test(a: ta);
begin
end;

procedure test(b: tb);
begin
end;

end.
***

The compiler can match the interface and implementation definitions  
based on the fact that the type definitions match (note, "type  
definitions" and not "type names" — i.e., you can add "type tc = ta"  
and change the "a: ta" into "a: tc" only in the implementation, and it  
will still compile because the type definitions still match).

However, if you would have

   procedure "test(a: array[0..10])"

in the interface, there would be no way for the compiler to match the  
implementation to the interface because in both cases a new definition  
would be created. This is a fundamental aspect of how Pascal's type  
system works.

> Remember
> that you can use "open array" in function params anyway.

An "open array" is a special case. It even can only appear inside  
parameter lists, and there is no way to define a standalone "open  
array" type.


Jonas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130403/41a565d5/attachment.html>


More information about the fpc-pascal mailing list