Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

Idiom #269 Enum to String

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

  e := horse;
  writestr(s, e);
  writeln(e);
T e = T.Horse;
string s = e.ToString();
Console.WriteLine(s);
toString(Atom) when Atom == bike orelse Atom == car orelse Atom == horse ->
        erlang:atom_to_list(Atom).

%% E = horse, S = toString(E).
var e = T.HORSE;
var s = e.name();
System.out.println(s);
val e = t.values().random() // or t.bike, but that's boring
val s = e.name
println(s)
e = T.horse
s = e.name
print(s)
let e = t::bike;
let s = format!("{:?}", e);

println!("{}", s);

New implementation...
Bart