version in enum
Hi, Why doesn't this work? Seems like it should: enum { A = 1, version(xx) { B = 2 } } void main() { } Compilation output: /d732/f174.d(5): Error: basic type expected, not version /d732/f174.d(5): Error: no identifier for declarator int /d732/f174.d(5): Error: if type, there must be an initializer /d732/f174.d(5): Error: found 'version' when expecting ',' /d732/f174.d(5): Error: basic type expected, not ( /d732/f174.d(5): Error: function declaration without return type. (Note that constructors are always named 'this') /d732/f174.d(5): Error: no identifier for declarator int(xx) /d732/f174.d(5): Error: if type, there must be an initializer /d732/f174.d(5): Error: found '{' when expecting ',' /d732/f174.d(8): Error: unrecognized declaration
Re: version in enum
On Saturday, 9 January 2016 at 12:43:32 UTC, Øivind wrote: Hi, Why doesn't this work? Seems like it should: enum { A = 1, version(xx) { B = 2 } } It's not allowed in the grammar but I agree with you, it could be useful. Recent example where it could: --- enum VirtualKey { version(linux) VK_UP = 0; version(Win32) VK_UP = 1, } --- a solution is to define a manifest constant depending on your version() and to use a ternary expression in the enum definition. For me then this works: --- version(linux) enum ver = true; else enum ver = false; enum VirtualKey { VK_UP = (ver) ? 0 : 1, } --- Not ideal but it works.
Re: version in enum
On Saturday, 9 January 2016 at 12:43:32 UTC, Øivind wrote: Why doesn't this work? Seems like it should: D defines version to only work on *complete* blocks. You're trying to use it on a partial block there. You'll have to try something else. Perhaps copying the whole enum in the different version blocks
Re: version in enum
On Saturday, January 09, 2016 13:32:49 Adam D. Ruppe via Digitalmars-d-learn wrote: > On Saturday, 9 January 2016 at 12:43:32 UTC, Øivind wrote: > > Why doesn't this work? Seems like it should: > > D defines version to only work on *complete* blocks. You're > trying to use it on a partial block there. > > You'll have to try something else. Perhaps copying the whole enum > in the different version blocks Yeah. That's what we do in core.time.ClockType. You have to duplicate the entire enum for each version with each enum declaration listing exactly which members exist in that version, which is an annoying amount of code duplication, but unless you did something with string mixins to generate your enum, and it generated the actual declaration for you with all of the separate version blocks, you're stuck with that duplication. And I rather doubt that going to the effort of generating a string mixin just so that you can list each member exactly once is worth it. A related thing to consider (depending on what the enum is used for) is that with a differing list of enum members in each version, unless you specify the value of each enum member explictly, they'll differ in each version. In many cases, that probably doesn't matter, but it some, it would. So, it's something to think about when versioning an enum. - Jonathan M Davis
Re: version specific enum members
On Fri, 30 Oct 2009 06:38:44 -0500, Ary Borenszweig a...@esperanto.org.ar wrote: Phil Deets wrote: On Thu, 29 Oct 2009 18:28:12 -0500, Ary Borenszweig a...@esperanto.org.ar wrote: Ellery Newcomer wrote: Unfortunately, that's going to be about the best you can do, unless you're willing to play with string mixins and their ilk. Or unless you create an enhancement request. That seems a very valid one. Done (on the main D list). I meant a bugzilla enhancement, otherwise it'll get lost in the newsgroups. Well, I wanted to gauge support for the idea. It turns out I found a string mixin workaround that works; so it isn't critical for me now.
version specific enum members
Hi, is there a way to add members to an enum based on conditional compilation symbols. I tried enum Tag { A, B, version (symbol) { C, D, } E, } but it doesn't work. I know I could do version (symbol) { enum Tag { A, B, C, D, E } } else { enum Tag { A, B, E } } but I don't want to do that since in my case, there can be quite a few elements outside of the version, and I don't want to repeat them all. -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Re: version specific enum members
Phil Deets wrote: Hi, is there a way to add members to an enum based on conditional compilation symbols. I tried enum Tag { A, B, version (symbol) { C, D, } E, } but it doesn't work. I know I could do version (symbol) { enum Tag { A, B, C, D, E } } else { enum Tag { A, B, E } } but I don't want to do that since in my case, there can be quite a few elements outside of the version, and I don't want to repeat them all. Unfortunately, that's going to be about the best you can do, unless you're willing to play with string mixins and their ilk.
Re: version specific enum members
Ellery Newcomer wrote: Phil Deets wrote: Hi, is there a way to add members to an enum based on conditional compilation symbols. I tried enum Tag { A, B, version (symbol) { C, D, } E, } but it doesn't work. I know I could do version (symbol) { enum Tag { A, B, C, D, E } } else { enum Tag { A, B, E } } but I don't want to do that since in my case, there can be quite a few elements outside of the version, and I don't want to repeat them all. Unfortunately, that's going to be about the best you can do, unless you're willing to play with string mixins and their ilk. Or unless you create an enhancement request. That seems a very valid one.
Re: version specific enum members
On Thu, 29 Oct 2009 18:28:12 -0500, Ary Borenszweig a...@esperanto.org.ar wrote: Ellery Newcomer wrote: Phil Deets wrote: Hi, is there a way to add members to an enum based on conditional compilation symbols. I tried enum Tag { A, B, version (symbol) { C, D, } E, } but it doesn't work. I know I could do version (symbol) { enum Tag { A, B, C, D, E } } else { enum Tag { A, B, E } } but I don't want to do that since in my case, there can be quite a few elements outside of the version, and I don't want to repeat them all. Unfortunately, that's going to be about the best you can do, unless you're willing to play with string mixins and their ilk. Or unless you create an enhancement request. That seems a very valid one. Done (on the main D list).
Re: version specific enum members
Phil Deets wrote: Hi, is there a way to add members to an enum based on conditional compilation symbols. I tried enum Tag { A, B, version (symbol) { C, D, } E, } but it doesn't work. I know I could do version (symbol) { enum Tag { A, B, C, D, E } } else { enum Tag { A, B, E } } but I don't want to do that since in my case, there can be quite a few elements outside of the version, and I don't want to repeat them all. template Version(char[] ident) { mixin(`version(`~ident~`) const Version = true; else const Version = false;`); } char[] genEnum(bool flag1, bool flag2) { return A, B, ~ (flag1 ? C, D, : ) ~ E, F, ~ (flag2 ? G, H, : ) ~ G, ; } mixin(genEnum( Version!(`symbol`), Version!(`somethingElse`) )); ... is about the best you can do, I think.
Re: version specific enum members
On Fri, 30 Oct 2009 00:03:23 -0500, Daniel Keep daniel.keep.li...@gmail.com wrote: Phil Deets wrote: Hi, is there a way to add members to an enum based on conditional compilation symbols. I tried enum Tag { A, B, version (symbol) { C, D, } E, } but it doesn't work. I know I could do version (symbol) { enum Tag { A, B, C, D, E } } else { enum Tag { A, B, E } } but I don't want to do that since in my case, there can be quite a few elements outside of the version, and I don't want to repeat them all. template Version(char[] ident) { mixin(`version(`~ident~`) const Version = true; else const Version = false;`); } char[] genEnum(bool flag1, bool flag2) { return A, B, ~ (flag1 ? C, D, : ) ~ E, F, ~ (flag2 ? G, H, : ) ~ G, ; } mixin(genEnum( Version!(`symbol`), Version!(`somethingElse`) )); ... is about the best you can do, I think. Hey, thanks for the idea. I worked with it a little bit to remove the generator function and use multiline strings. This is what I came up with. mixin(qENUM enum Tag { A, B, ENUM~(Version!(symbol)?qENUM C, D, ENUM:)~qENUM E, } ENUM); That's not pretty, but it's good enough for me.