Peter Schorn wrote:
Dear all,
while porting from CodeWarrior Object Pascal to GPC I noticed the following differences which might be worthwhile to address in GPC.
- Procedure type assignment compatibility with specialized parameters
program prog2; { compile with gpc --mac-pascal prog2.pas } type baseType = object end;
derivedType = object(baseType) end;
procedure p1(x: baseType); begin end; procedure p2(x: derivedType); begin end; var pv: procedure(x: baseType); begin pv := p1; pv := p2; { error: incompatible types in assignment } end.
A workaround is to declare the x in p2 as baseType and perform a cast in the body of p2.
Assigning p2 to pv is unsafe (I allows breaking type rules). Consider:
program unsafe; type baseType = object end;
derivedType = object(baseType) procedure m; end;
procedure derivedType.m; begin end;
var pv: procedure(x: baseType); v: baseType;
procedure p2(x: derivedType); begin x.m end;
begin new(v); pv := p2; { error: prevents wrong call } pv(v) end .
The only place which prevents wrong call is assignment to pv. Do CodeWarrior accept the program above? Safe rule for procedural assignment like:
pv := p;
require types of arguments of pv be a supertype of arguments of p, so if pv has argument of derivedType and p has argument of baseType then the assignment is safe. ATM we require matching types, but in principle we could use weaker rule. It is not clear how usefull weaker rule is. OTOH Pascal requires matching types in some other context where weaker rules are safe (notably for array and pointer assignments), so it is not clear if it is worth the effort to implement weaker rule.