Re: [ooc-compiler] wrapper to SYSTEM.VAL
Brought to you by:
mva
|
From: Stewart G. <sgr...@ii...> - 2010-10-06 02:24:15
|
Hi Norayr, I think the restriction is correct. This can be confusing, but think of it like this: if your parameter is declared "aaa(VAR p : T1)" then it means the procedure can write ANY value of type T1 to p. Sure, T2 is a subtype of T1 but there are other subtypes of T1 which are not also T2. It would be an error for the compiler to allow aaa(p2) because you could then assign something to p2 which is not a subtype of T2. For example: PROCEDURE aaa (VAR p : T1); BEGIN NEW(p); (* assign new instance of T1 to p *) END aaa; Now, allowing aaa(p2) would break the type system. Its hard to say how to best handle this without knowing some more details of what you're trying to do. This sort of pattern could work: VAR p1 : T1; ... p1 := NEW(T2); aaa(p1); Cheers, Stewart On 6/10/10 1:18 AM, Norayr Chilingaryan wrote: > Hello, yet another question which came out during library porting work. > MODULE test2; > > TYPE T1 = POINTER TO TRec; > TRec = RECORD > a : INTEGER > END; > > TYPE T2 = POINTER TO T2Rec; > T2Rec = RECORD (TRec) > b : REAL > END; > VAR p1 : T1; > p2 : T2; > > PROCEDURE aaa (VAR p : T1); > BEGIN > > END aaa; > > BEGIN > p2 := NEW(T2); > p1 := NEW(T1); > aaa(p2); > > END test2. > > So, this won't compile unless: > we change procedure to PROCEDURE aaa (p : T1); i. e. without VAR > or > pointer passed to the procedure is not a pointer to the extended type, > so aaa(p1) will work. > > Are that restrictions intentional? > What would be an optimal (or nicer) workaround to solve this if I need > to compile library which have been written like this. > > Thank you > Norayr [...] |