How to declare generic type restriction for enums? lazarus / 21-General ---------------------------------------------------------------------- Eugene Loza · 2024-08-14 18:01 I wonder if there is a way to put restriction on generic type to be an enum? I mean, the code below compiles and works exactly as expected, but I wonder if it can be made more me-proof (aka fool-proof) to make sure it's never specialized for something that cannot be asked to for I in T or GetEnumName? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i generic function EnumToStr(const EnumValue: T): String;begin  Exit(GetEnumName(TypeInfo(T), Ord(EnumValue)));end;generic function StrToEnum(const EnumString: String): T;var  I: T;begin  for I in T do    if specialize EnumToStr(I) = EnumString then      Exit(I);  raise EEnumConversionError.CreateFmt('Cannot find %s value: "%s"', [PTypeInfo(TypeInfo(T))^.Name, EnumString]);end; I see how to make that for more complex types, but specifically for enums? ---------------------------------------------------------------------- Thaddy · 2024-08-14 19:11 This is not yet possible, although there is a feature request for it. As I understand that is quite well received by the core developers, but, as usual, time and priorities are the issue. Enums and ordinals in general: the proposal is for ordinals, which includes enums of course. Although enums with assigned values will likely not be supported. PascalDragon has responded to a similar post, and/or an f.r on the bugtracker, afaik.