Logo

Programming-Idioms

  • Kotlin
  • C++
  • Python

Idiom #269 Enum to String

Given the enumerated type t with 3 possible values: bike, car, horse.
Set the enum value e to one of the allowed values of t.
Set the string s to hold the string representation of e (so, not the ordinal value).
Print s.

val e = T.BIKE
val s = e.name
println(s)
e = T.horse
s = e.name
print(s)
with Ada.Text_IO;
declare
   type T is (Bike, Car, Horse);
   E : constant T      := Horse;
   S : constant String := T'Image (E);
begin
   Ada.Text_IO.Put_Line (S);
end;

New implementation...
< >
Bart