[fpc-devel] Re: [fpc-announce] Feature announcement: Type helpers

Gerhard Scholz gs at g--s.de
Sat Feb 9 11:19:17 CET 2013


I know that the examples are stupid and most of them senseless.

Sometimes I used similar workarounds (more senseful),
so I just wanted to know if the type helper construct produces more
efficient code.
It doesn't.

> The main advantage of helpers compared to your example is that the
compiler will do the type checking.

That's an advantage.

>>  12345678.showvalue ;     // 4712?
>And the last line should show 12345678 hopefully :)

Yes. it does.

Greetings

Gerhard

----- Original Message ----- 
From: Sven Barth
To: FPC developers' list
Sent: Friday, February 08, 2013 8:24 PM
Subject: Re: [fpc-devel] Re: [fpc-announce] Feature announcement: Type
helpers


Am 08.02.2013 19:52 schrieb "Gerhard Scholz" <gs at g--s.de>:
>
> 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).
Impressive O.o I would have never thought to use objects like that. The main
advantage of helpers compared to your example is that the compiler will do
the type checking.
> 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.
If we could keep track of writes to Self and then mark the methods
accordingly we could at least provide a warning or a hint.
And the last line should show 12345678 hopefully :)
Regards,
Sven



_______________________________________________
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