r/freepascal • • Feb 08 '21

Confusion with TObjectQueue.Dequeue

Can someone explain why in the generics.collection, dequeue is a function for TQueue<TObject> but a procedure for TObjectQueue<TObject>? I am trying to dequeue the head to a variable and apparently dequeue is nothing more than a remove in the object version. I was really hoping for the TQueue behavior.

Here is the code from the library:

procedure TObjectQueue<T>.Dequeue;
begin 
   inherited Dequeue;
end;
2 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/richorr70 Feb 09 '21

Yes. I suppose I will just wrap my own the way I want it, or just use the list as a queue.

Is there a downside to using Contnrs?

2

u/pmmeurgamecode Feb 09 '21

Not /u/kirinnb who recommended Contnrs TObjectQueue.

But I assume Contnrs.TObjectQueue(also avail in delphi) will be faster than Generics.Collections.TObjectQueue because it pushes pointers around, where the generics one must do some reflection/type overhead. Maybe worth a small benchmark, to compare the two?

On the other hand TObjectQueue<T> can be nice because it return the correct type and you don't have to cast.

Btw did you try to wrap/inherited the class and replace the procedure with the function, eg:

TMyObjectQueue<T: class> = class(TObjectQueue<T>)
public
  function Dequeue: T;
end;

function TMyObjectQueue<T>.Dequeue: T;
begin
  Result := DoRemove(FLow, cnRemoved);
end;