digitalmars.D.learn - %s not producing string representation of enum?
- Shriramana Sharma (11/11) Dec 10 2015 Hello. I'm using DMD 2.069.2. As per http://ddili.org/ders/d.en/enum.htm...
- Daniel Kozak via Digitalmars-d-learn (15/28) Dec 10 2015 V Thu, 10 Dec 2015 19:54:43 +0530
- Daniel Kozak via Digitalmars-d-learn (5/18) Dec 10 2015 V Thu, 10 Dec 2015 19:54:43 +0530
- Basile B. (31/42) Dec 10 2015 You should rather use std.traits.EnumMembers to iterate the
- Basile B. (18/34) Dec 10 2015 By the way I should use format() in the example, but writeln()
- Shriramana Sharma (9/11) Dec 10 2015 Wow this is great! I was thinking that enum.max + 1 is not really befitt...
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/9) Dec 11 2015 Done[1]:
- Shriramana Sharma (4/5) Dec 11 2015 Ali that's great! Thank you!
Hello. I'm using DMD 2.069.2. As per http://ddili.org/ders/d.en/enum.html the following code is supposed to output the *names* of the suits: import std.stdio; void main() { enum Suit { spades, hearts, diamonds, clubs } foreach (suit; Suit.min .. Suit.max + 1) { writefln("%s", suit); } } But I'm getting 0 1 2 3. Kindly advise. --
Dec 10 2015
V Thu, 10 Dec 2015 19:54:43 +0530 Shriramana Sharma via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> napsáno:Hello. I'm using DMD 2.069.2. As per http://ddili.org/ders/d.en/enum.html the following code is supposed to output the *names* of the suits: import std.stdio; void main() { enum Suit { spades, hearts, diamonds, clubs } foreach (suit; Suit.min .. Suit.max + 1) { writefln("%s", suit); } } But I'm getting 0 1 2 3. Kindly advise.void main() { writeln(typeid(typeof(Suit.max))); writeln(typeid(typeof(Suit.max + 1))); } + 1 makes int from Suit import std.stdio; import std.conv; foreach (suit; Suit.min .. Suit.max + 1) { enum Suit { spades, hearts, diamonds, clubs } writefln("%s", to!Suit(suit)); }
Dec 10 2015
V Thu, 10 Dec 2015 19:54:43 +0530 Shriramana Sharma via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> napsáno:Hello. I'm using DMD 2.069.2. As per http://ddili.org/ders/d.en/enum.html the following code is supposed to output the *names* of the suits: import std.stdio; void main() { enum Suit { spades, hearts, diamonds, clubs } foreach (suit; Suit.min .. Suit.max + 1) { writefln("%s", suit); } } But I'm getting 0 1 2 3. Kindly advise.or: foreach (suit; Suit.min .. Suit.max + cast(Suit)1) { ... }
Dec 10 2015
On Thursday, 10 December 2015 at 14:24:43 UTC, Shriramana Sharma wrote:Hello. I'm using DMD 2.069.2. As per http://ddili.org/ders/d.en/enum.html the following code is supposed to output the *names* of the suits: import std.stdio; void main() { enum Suit { spades, hearts, diamonds, clubs } foreach (suit; Suit.min .. Suit.max + 1) { writefln("%s", suit); } } But I'm getting 0 1 2 3. Kindly advise.You should rather use std.traits.EnumMembers to iterate the members of an enum: ~~~~~~~~~~~~ import std.stdio; void main(string[] args) { import std.traits: EnumMembers; enum Suit { spades, hearts, diamonds, clubs } foreach(member; EnumMembers!Suit) writeln(member); } ~~~~~~~~~~~~ it can also counts ~~~~~~~~~~~~ import std.stdio; void main(string[] args) { import std.traits: EnumMembers; enum Suit { spades = 23.2, hearts, diamonds, clubs } foreach(i,member; EnumMembers!Suit) { writeln(member); writeln(i); } } ~~~~~~~~~~~~ which is interesting to get the rank of a member, considering that the rank is not always the same as the value (like here, it's a float ;) ).
Dec 10 2015
On Thursday, 10 December 2015 at 14:46:26 UTC, Basile B. wrote:On Thursday, 10 December 2015 at 14:24:43 UTC, Shriramana Sharma wrote:By the way I should use format() in the example, but writeln() use the same methods: ~~~~~~~~~~~~ import std.stdio; void main(string[] args) { import std.traits: EnumMembers; enum Suit { spades = 23.2, hearts = 8.3, diamonds = 12.56, clubs = 1.3 } foreach(i,member; EnumMembers!Suit) { writefln("%s", member); writefln("%f", member); writeln(i); } } ~~~~~~~~~~~~Hello. I'm using DMD 2.069.2. As per http://ddili.org/ders/d.en/enum.html the following code is supposed to output the *names* of the suits: import std.stdio; void main() { enum Suit { spades, hearts, diamonds, clubs } foreach (suit; Suit.min .. Suit.max + 1) { writefln("%s", suit); } } But I'm getting 0 1 2 3. Kindly advise.You should rather use std.traits.EnumMembers to iterate the
Dec 10 2015
Basile B. wrote:You should rather use std.traits.EnumMembers to iterate the members of an enum:Wow this is great! I was thinking that enum.max + 1 is not really befitting D's elegant approach to programming. Ali should really update that section of his book to use EnumMembers. This will both help avoid the above hack and only then will his reference to %s and %d having different effects for enums be true... Thanks once more to Ali for his book and you people for the replies! --
Dec 10 2015
On 12/10/2015 08:58 PM, Shriramana Sharma wrote:Ali should really update that section of his book to use EnumMembers.Done[1]: http://ddili.org/ders/d.en/enum.html#ix_enum.EnumMembers,%20std.traits Thank you, Ali [1] https://bitbucket.org/acehreli/ddili/commits/5f146bdc921c6e82c10a107906bd45fa325b0e82#chg-src/ders/d.en/preface.d
Dec 11 2015
Ali Çehreli wrote:http://ddili.org/ders/d.en/enum.html#ix_enum.EnumMembers,%20std.traitsAli that's great! Thank you! --
Dec 11 2015