Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 11, 2009 enum to string | ||||
---|---|---|---|---|
| ||||
Is there any way to do this (preferably in D1) with reflection? (ie, without having to manually create a conversion func/lookup for every value of every enum.) ---------------------- enum Shape { Square, Circle } char[] foo(Shape s) { // ????? } // Either one of these, I don't really care which assert(foo(Shape.Square) == "Shape.Square"); assert(foo(Shape.Square) == "Square"); ---------------------- |
March 11, 2009 Re: enum to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | In D2: enum Shape: string { Square = "Square", Circle = "Circle", } void main() { assert(cast(string) Shape.Square == "Square"); } |
March 11, 2009 Re: enum to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky |
Nick Sabalausky wrote:
> Is there any way to do this (preferably in D1) with reflection? (ie, without having to manually create a conversion func/lookup for every value of every enum.)
>
> ----------------------
> enum Shape
> {
> Square, Circle
> }
> char[] foo(Shape s)
> {
> // ?????
> }
>
> // Either one of these, I don't really care which
> assert(foo(Shape.Square) == "Shape.Square");
> assert(foo(Shape.Square) == "Square");
> ----------------------
The only way that I know of is to generate foo at the same time as Shape. Generally, you write a CTFE function that generates the enum and also generates the toString function.
-- Daniel
|
March 11, 2009 Re: enum to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to MIURA Masahiro | "MIURA Masahiro" <echochamber@gmail.com> wrote in message news:gp730i$30e8$1@digitalmars.com... > In D2: > > enum Shape: string > { > Square = "Square", > Circle = "Circle", > } > > void main() > { > assert(cast(string) Shape.Square == "Square"); > } So there's no built-in way to do this with compile-time reflection? I suppose I could work around it by creating a mixin that automatically generates both the enum and an enum->string routine. |
March 11, 2009 Re: enum to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Tue, Mar 10, 2009 at 9:54 PM, Nick Sabalausky <a@a.a> wrote:
> "MIURA Masahiro" <echochamber@gmail.com> wrote in message news:gp730i$30e8$1@digitalmars.com...
>> In D2:
>>
>> enum Shape: string
>> {
>> Square = "Square",
>> Circle = "Circle",
>> }
>>
>> void main()
>> {
>> assert(cast(string) Shape.Square == "Square");
>> }
>
> So there's no built-in way to do this with compile-time reflection?
>
> I suppose I could work around it by creating a mixin that automatically generates both the enum and an enum->string routine.
In D1, that's the way you have to do it.
In D2, there is more reflection - I know you can use
__traits(allMembers, Shape) to get the names? Or something like that?
It's completely undocumented, of course. There's also std.typecons
which has an enum generator that will create toString and I think
fromString functions as well.
|
March 12, 2009 Re: enum to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> There's also std.typecons
> which has an enum generator that will create toString and I think
> fromString functions as well.
Which should be easily portable to D1.
|
March 12, 2009 Re: enum to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky Attachments: | Nick Sabalausky wrote: > Is there any way to do this (preferably in D1) with reflection? (ie, without having to manually create a conversion func/lookup for every value of every enum.) > > ---------------------- > enum Shape > { > Square, Circle > } > char[] foo(Shape s) > { > // ????? > } > > // Either one of these, I don't really care which > assert(foo(Shape.Square) == "Shape.Square"); > assert(foo(Shape.Square) == "Square"); > ---------------------- > You got to give it to Walter: the code is pretty clean (for C++ code anyway) Add the attached code to src\dmd\mtype.c, line 5025 (in TypeEnum::dotExp, after the #endif). This now works: import std.stdio; enum ABC { A, B, C }; void main(string[] args) { foreach(a; ABC.tupleof) { writefln(a); } } prints: A B C Of course, I'm way over my head here. I've just copied the code from struct.tupleof to make enum.tupleof and creating a tuple of StringExps insteadof DotVarExps, whatever they are.... L. |
March 12, 2009 Re: enum to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Oops, sorry, I didn't notice this was the .learn newsgroup. I guess a patch to the DMD source was not really what you expected. I just got so anxious! L. |
March 12, 2009 Re: enum to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | "Lionello Lunesu" <lio@lunesu.remove.com> wrote in message news:gpc4j3$30a0$2@digitalmars.com... > Oops, sorry, I didn't notice this was the .learn newsgroup. I guess a patch to the DMD source was not really what you expected. I just got so anxious! > > L. Hey, solutions are solutions :) In fact I may very well try that out. I'm not particularly keen on using an ugly mixin to declare all my enums just so that I can use a simple reflection feature like that (which I'd mostly just be using for debugging info anyway). |
March 12, 2009 Re: enum to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | "Lionello Lunesu" <lio@lunesu.remove.com> wrote in message news:gpc4es$30a0$1@digitalmars.com... > Nick Sabalausky wrote: >> Is there any way to do this (preferably in D1) with reflection? (ie, >> without >> having to manually create a conversion func/lookup for every value of >> every >> enum.) >> >> ---------------------- >> enum Shape >> { >> Square, Circle >> } >> char[] foo(Shape s) >> { >> // ????? >> } >> >> // Either one of these, I don't really care which >> assert(foo(Shape.Square) == "Shape.Square"); >> assert(foo(Shape.Square) == "Square"); >> ---------------------- >> > > You got to give it to Walter: the code is pretty clean (for C++ code anyway) > > Add the attached code to src\dmd\mtype.c, line 5025 (in TypeEnum::dotExp, after the #endif). This now works: > > import std.stdio; > enum ABC { A, B, C }; > > void main(string[] args) > { > foreach(a; ABC.tupleof) > { > writefln(a); > } > } > > prints: > A > B > C > > Of course, I'm way over my head here. I've just copied the code from struct.tupleof to make enum.tupleof and creating a tuple of StringExps insteadof DotVarExps, whatever they are.... > > L. > Your attachment is zero-byte. |
Copyright © 1999-2021 by the D Language Foundation