@Kays
You forgot to exit early so the loops run inefficient. It is also wrong: it keeps the last in sample, not the first it encounters.
How about simply:
{$ifdef fpc}{$mode delphi}{$endif}
ype
TRange = 0..255;
TSet = set of TRange;
TColor = (red,white,blue,yellow,magenta,green);
TColors = set of TColor;
function bsrset<_S,_R>(const value:_S):integer;inline;
var r:_R;
begin
result := -1;
// first found is lsb
if GetTypeKind(_S) = tkSet then
for r in value do exit(ord(r));
end;
function bsfset<_S,_R>(const value:_S):integer;inline;
var r:_R;
begin
result := -1;
// first found is msb
if GetTypeKind(_S) = tkSet then
for r := High(r) downto low(r) do
if r in value then exit(ord(r));
end;
var
a:Tset = [4,8,9,33];
c:Tcolors =[white,blue,magenta];
begin
writeln(bsrset<TSet,TRange>(a));
writeln(bsfset<TSet,TRange>(a));
writeln(bsrset<Tcolors,TColor>(c));
writeln(bsfset<Tcolors,TColor>(c));
end.
Maybe slow but sure and short.
Come to think of it, also check if _R is an enumerable type, like [tkEnumeration, TChar, tkInteger].
Will add that later.
I think this is the easiest, not the fastest.