I have a section of code where I need to modify the top record on a stack. This code is supposed to be highly performant, so I would like to optimize it as much as possible. I would like to get the top record of the stack without popping and pushing it. The record is somewhat complex, so copying the data back and forth to a temporary record is silly. A sample program is below
type
TMyRec=record
s:string;
end;
PMyRec=^TMyrec;
var
stk:TStack<TMyRec>;
rec:TMyRec;
prec:pMyRec;
begin
stk:=TStack<TMyRec>.Create;
rec.s:='Hello';
stk.push(rec);
prec:=@stk.Peek; // get error “variable required”
prec.s:=’Goodbye’;
stk.Free;
end;
Seems like @stk.Peek is returning the address to the Peek method, not the pointer of the element being returned by the peek method. I have tried casting in various ways but it never compiles properly. Any ideas?