Gorilla3D

Blogs of my work and thoughts

Free Pascal Templates / Generics / Containers

,

No matter what you want to call them Free Pascal has had them since 2.2, before Embarcadero's Delphi compiler had them. Creating your own generics in Free Pascal can be a bit cumbersome especially when you just want a container to use. So FPC has created the FGL unit that has the basics to get real work done. Here is an example:

{ Compile with "fpc list.pas" }
program list;

{ fgl contains the templates we need. }
uses
	fgl;

{ We have to create a type using the template }
Type  
	TIntegerList = specialize TFPGList<Integer>;

var
	{ Now we have a list container! }
	NumberList : TIntegerList;
	I, Item : Integer;
begin
	I := 1;
	{ Create the List }
	NumberList := TIntegerList.Create;

	{ Add some stuff to the List }
	NumberList.Add(I);
	NumberList.Add(23);

	{ Iterate through the list }
	for Item in NumberList do
	begin
		WriteLn('Item: ', Item);
	end;

	{ Destroy the List }
	NumberList.Destroy;
end.

Simple Microsecond Timers in ValaMy First iPhone Game Part#1