[fpc-pascal] Untyped var params

Ryan Joseph ryan at thealchemistguild.com
Sun Jan 8 10:35:07 CET 2017


> On Jan 8, 2017, at 2:37 AM, Andrew Hall <andrew.hall at shaw.ca> wrote:
> 
> If you cast your “something” to a typed pointer, the compiler will do the work for you in the usual way:
> 
> PDouble(something)^ := myDouble;
> PAnsiString(something)^ := myAnsiString;
> myRecordStructure  := PMyRecordStructure(something)^;

I’m not getting any of this to work. 

Here is the pattern I was attempting. It’s a “release existing, retain and assign new” function which handles nil inputs safely. I thought I could use untyped params so I don’t need to typecast “io” to TObject when I call RetainObject every time.

procedure RetainObject (var io; newObject: TObject);
var
	obj: TObject absolute io;
begin
	if obj <> nil then
		obj.Release;
	if newObject <> nil then
		begin
			obj := newObject;
			obj.Retain;
		end
	else
		obj := nil;
	
	TObjectPtr(io)^ := obj;	// crashes here
end;


var
	foo: TMyObject;
	something: TMyObject;

foo := TMyObject.Create;
RetainObject(something, foo);


Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list