Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an anonymous enumerated type into a subroutine?

I'm trying to make a convenient function to convert a System.Classes.TShiftState into a user-readable string. To make it easier, I've made a subroutine to perform common code, to make the function more compact.

The problem is, I cannot figure out how to pass one of the TShiftState enum types into this subroutine. I tried Byte, Integer, and Cardinal but I keep getting Incompatible types: 'Byte' and 'Enumeration' (or whichever type I was trying). Hovering over one of them only shows $1 where the type would usually be.

function ShiftStateStr(const Shift: TShiftState): String;
  procedure A(const Sh: Byte; const Str: String);
  begin
    if Sh in Shift then
      Result:= Result + StrLen(Str, Length(Str)+1)
    else
      Result:= Result + StrLen('', Length(Str)+1);
  end;
begin
  Result:= '';
  A(ssShift, 'Shift');
  A(ssAlt, 'Alt');
  A(ssCtrl, 'Ctrl');
  A(ssLeft, 'Left');
  A(ssRight, 'Right');
  A(ssMiddle, 'Middle');
  A(ssDouble, 'Double');
  A(ssTouch, 'Touch');
  A(ssPen, 'Pen');
  A(ssCommand, 'Cmd');
  A(System.Classes.ssHorizontal, 'Horz');
end;

NOTE: StrLen is a separate function which pads a string with spaces of a given length.

TShiftState is defined in System.Classes like so:

type
  TShiftState = set of (ssShift, ssAlt, ssCtrl,
    ssLeft, ssRight, ssMiddle, ssDouble, ssTouch, ssPen, ssCommand, ssHorizontal);

How can I properly pass this into the A subroutine?

like image 607
Jerry Dodge Avatar asked Nov 19 '16 02:11

Jerry Dodge


1 Answers

Change first parameter of A to const Sh: TShiftState. Then change each call to A into the form

A([ssShift], 'Shift');

and finally the condition test into

if Sh <= Shift then

Ref. Expressions

X <= Y is True just in case every member of X is a member of Y

like image 114
Tom Brunberg Avatar answered Sep 24 '22 08:09

Tom Brunberg