I keep butting my head into the lack of an enumeration generics constraint.
I can’t make generic arrays or set types from an enumeration, nor do operations on a variable of that type.
(Disclaimer: The code is for illustration only and may be incorrect or illogical)
TTaskFactory = class
type
TTaskClassArray: Array [T] of TTaskClass; //<– illegal
TTaskSet: Set of T; //<– illegal
private
class var FFactory: TTaskClassArray;
protected
FAllowedTasks: TTaskSet;
public
class procedure RegisterTask(const Id: T; aTaskClass:TTaskClass);
function MakeTask(const Id:T):TTask;
property AllowedTasks read FAllowedTasks write FAllowdTasks;
end;
class procedure TTaskFactory.RegisterTask(const Id: T; aTaskClass:TTaskClass);
begin
Log(‘Registered Task Id ‘+IntToStr(Ord(ID))); //<– illegal
FFactory[Id] := aTaskClass; //<– illegal
end;
function TTaskFactory.MakeTask(const Id:T):TTask;
begin
if Id in AllowedTasks //<– illegal
then begin
if Assigned(FFactory[Id]) //<– illegal
then Result := FFactory[Id].Create //<– illegal
else raise EProgrammerNotFound.Create(‘Forgot to register’);
end
else Result := nil;
end;
If only I could write:
TTaskFactory = class
TSoTaskType = (ttMakeItSo, ttSoBeIt, … ttSoWhat)
TSoTaskFactory = class(TTaskFactory);
I am sure there are other examples, but I really miss being able to do
declarations and operations like these in a generic class:
Arrays, sets, low/high, ord, in, pred/succ, and related operations.
Yes, there are better ways to do class factories, and I use them – but the Enumeration constraint would enable so many possibilities.
You must be logged in to post a comment.