Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

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.

import static java.lang.System.out;
T e = Car;
String s = e.name();
out.println(s);
var e = T.HORSE;
var s = e.name();
System.out.println(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;
T e = T.Horse;
string s = e.ToString();
Console.WriteLine(s);
var e = t.bike;
var s = e.name;
print(s);
toString(Atom) when Atom == bike orelse Atom == car orelse Atom == horse ->
        erlang:atom_to_list(Atom).

%% E = horse, S = toString(E).
val e = T.BIKE
val s = e.name
println(s)
  e := horse;
  writestr(s, e);
  writeln(e);
use enum qw(bike car horse);

my $e = horse;
print $e;
use enum     qw(bike car horse);
my @enum_T = qw(bike car horse);

my $e = horse;
my $s = $enum_T[$e];
print $s;
e = T.horse
s = e.name
print(s)
let e = t::bike;
let s = format!("{:?}", e);

println!("{}", s);

New implementation...
< >
Bart