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

Sven Barth pascaldragon at googlemail.com
Fri Feb 8 20:24:14 CET 2013


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20130208/9c306402/attachment.html>


More information about the fpc-devel mailing list