Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
function TryEnumToStr(const s: string; out enum: T): Boolean;
var
code: integer;
begin
Val(s, enum, code);
Result := (code = 0);
end;
t = T[s]
T.values.byName(s);
T x = T.valueOf(s);
def try_str_to_enum(t, string), do: t[string]
my %T = ( RED => 1, GREEN => 2, BLUE => 3 );
sub TryStrToEnum { my $s = shift; $T{uc $s} }
print GREEN; # prints 2
print TryStrToEnum('BLUE'); prints 3