[fpc-devel] Re: [fpc-announce] Feature announcement: Type helpers
Gerhard Scholz
gs at g--s.de
Fri Feb 8 19:50:33 CET 2013
Thanks for the help, I understood it now; I also found the test progs.
As far I see, it could already be substituted by the following constructs:
program RHelper1v ;
type
poLongint = ^ oLongint ;
oLongint = object
li : longint ;
procedure ShowValue ;
procedure put ( j : longint ) ;
Function inc : poLongint ;
end ;
procedure oLongInt.showvalue ;
begin
Writeln ( 'Value=', li ) ;
end ;
procedure oLongInt.put ( j : longint ) ;
begin
li := j ;
end ;
function oLongInt.inc : polongint ;
begin
system.inc ( li ) ;
result := @self ;
end ;
function v ( i : longint ) : polongint ;
begin
result := addr(i) ;
end ;
var
i : LongInt ;
begin
i := 3 ;
olongint(i).showvalue ;
olongint(i).put ( 4711 ) ;
olongint(i).showvalue ;
olongint(i).inc ;
olongint(i).showvalue ;
olongint(v(12345678)^).put ( 4711 ) ; // senseless, but possible
olongint(v(12345678)^).inc ; // senseless, but possible
olongint(v(12345678)^).showvalue ; // 4712?
end.
And it produces the same assembler code as the construct TYPE HELPER FOR
LONGINT
(which is definitely better readable).
The same program with type helper:
program RHelper1f ;
type
pLongInt = ^ longint ;
tLongIntHelper = type helper for LongInt
procedure ShowValue ;
procedure put ( j : longint ) ;
Function inc : pLongint ;
end ;
procedure TLongIntHelper.showvalue ;
begin
Writeln ( 'Value=', self ) ;
end ;
procedure TLongIntHelper.put ( j : longint ) ;
begin
self := j ;
end ;
function tLongInthelper.inc : plongint ;
begin
system.inc ( self ) ;
result := @self ;
end ;
var
i : LongInt ;
begin
i := 3 ;
i.showvalue ;
i.put ( 4711 ) ;
i.showvalue ;
i.inc ;
i.showvalue ;
12345678.put ( 4711 ) ; // senseless, but possible
12345678.inc ; // senseless, but possible
12345678.showvalue ; // 4712?
end.
The last 3 lines show that the type helpers allow useless code. I assume
such useless code is not catchable by the compiler.
Gerhard
----- Original Message -----
From: "Paul Ishenin" <paul.ishenin at gmail.com>
To: "FPC developers' list" <fpc-devel at lists.freepascal.org>
Sent: Friday, February 08, 2013 3:21 PM
Subject: Re: [fpc-devel] Re: [fpc-announce] Feature announcement: Type
helpers
> 08.02.13, 21:52, Gerhard Scholz wrote:
>
>> Maybe I didn't understand the syntax correctly: I didn't see how to get
>> the value inside the method?
>
> By accessing Self
>
>> example:
>>
>> type
>> TLongIntHelper = type helper for LongInt
>> class procedure ShowValue; static;
>> end;
>>
>> class procedure TLongIntHelper.Test;
>> begin
>> Writeln('Value=',self);
>> end;
>
> Replace your static class procedure with regular method:
>
> procecure TLongIntHelper.Test;
> begin
> WriteLn(Self);
> end;
>
> Class static method is only needed if you are going to call it for the
> type declaration itself like: LongInt.PrintSize:
>
> class procedure TLongIntHelper.PrintSize;
> begin
> WriteLn(SizeOf(LongInt));
> end;
>
> static methods don't have a magic Self variable.
>
> In any case I suggest to look for test which had been commited together
> with Sven patch.
>
> Best regards,
> Paul Ishenin
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
More information about the fpc-devel
mailing list