Sunday, 15 August 2010

Printing the enum string values (not keys) in D -


I print "F" not "Fahrenheit" in this code. How can I not print the enum value key?

Import std.stdio; Flat ("% s", temperature unit F) written in zero main () {temperature temperature unit: string {C = "Celsius", F = "Fahrenheit"}; }

Just enter in a string:

  Listflone ​​("% s", cast (string) temperature unit. F);  

Note that it is not required when specifying cast string:

  string s = TemperatureUnit.F; // just works  

This only comes into play here because the family of std.stdio.write * function, as well as std. Conv For , look at the exact type given on it and try to be an expert on it when printing the name is useful when it is the enum color (red, green, blue) . But this behavior is less useful when the value is also a printable string, casting tells the function that you want to be treated as a plain string rather than an element.


No comments:

Post a Comment