Hacking "method variables"

Frank Heckenbach ih8mj at fjf.gnu.de
Mon Jun 12 11:33:23 CEST 2006

Forwarded from marcov at stack.nl (Marco van de Voort):

In gmane.comp.compilers.gpc, you wrote:
> Frank Heckenbach wrote:
>> begin
>>   bla:=xx.methodname;
>> end;
> So IIUC, the difference to Markus' way is that here the procedural
> variable also contains the object, while there it only points to the
> method, and can be applied to any object (of matching type), right?

Yes, though object-instance might be a better phrasing. It works though with
uninitialised objects too as long as you don't actually "use" (dereference)

{$mode delphi}

type tmyobject = class
                   function bla ( x: integer):integer;

     tmethodvar = function  (x:integer):Integer of object;

function tmyobject.bla(x:integer):Integer;
  writeln(x, ' ', PtrInt(self)); // ptrint is guaranteed pointer size compat

var x : TMyobject;
    y : tMethodVar;

  x:=nil;       // if you can detect uninitialised vars, this is not

will write  

5 0

I'm a bit worried that this syntax is a bit focused on object types that are
already (implicitely) a pointer, like Delphi.

If this is possible with (one of ) your object models, better warn if you
try to assign a methodvar to a method of an object that is on the stack
instead of on the heap.

More information about the Gpc mailing list