digitalmars.D - Enums can be annoyingly verbose...
- Regan Heath (32/32) Aug 09 2004 I have mentioned this before, but I kinda want some more opinions on the...
-
Carlos Santander B.
(21/21)
Aug 09 2004
"Regan Heath"
escribió en el mensaje - Jarrett Billingsley (8/8) Aug 09 2004 or you can do it like this.
- Regan Heath (15/23) Aug 09 2004 Thanks, I didn't realise you could have un-named enums.
- Deja Augustine (11/47) Aug 09 2004 The reason that it requires a fully qualified name is to prevent name
- Regan Heath (16/64) Aug 09 2004 An excellent question. The answer, this requires a fully qualified name.
- Deja Augustine (17/98) Aug 09 2004 Well, personally, I think that Walter has far far more important issues
-
Deja Augustine
(5/100)
Aug 09 2004
- Regan Heath (31/129) Aug 10 2004 I agree completely. I am not suggesting this be done 'right now' if you
- Sean Kelly (36/58) Aug 11 2004 But if the 'FooBarBaz' is omitted then the user does not know which enum...
- Regan Heath (35/112) Aug 11 2004 Yes they do.. think about how you'd go about it, first you'd look up the...
- Arcane Jill (37/66) Aug 11 2004 Yes you did. Or at least, your original idea, as I now understand it, co...
- Regan Heath (19/113) Aug 16 2004 True..
- Arcane Jill (43/54) Aug 16 2004 Me, personally?
- Regan Heath (6/16) Aug 17 2004 But of course! I simply wasn't thinking in D!
-
Carlos Santander B.
(15/15)
Aug 11 2004
"Jarrett Billingsley"
escribió en el mensaje - Arcane Jill (15/17) Aug 10 2004 Would that work? I mean, I can see that:
- Regan Heath (37/56) Aug 10 2004 Good question.. I think Walter is the only one who can really answer it,...
- Arcane Jill (14/16) Aug 11 2004 I take pride in being lazy. When I was studying math at school, I was ta...
- Ant (5/9) Aug 11 2004 and of course
- Peter Wood (3/19) Aug 11 2004 Give the hardest job to the laziest person and they will find the quicke...
- Walter (16/27) Aug 13 2004 taught
- Ilya Minkov (3/8) Aug 13 2004 I would like to silently thumb up the remark and ideas as well.
- Andy Friesen (16/56) Aug 11 2004 I don't think it's a good idea to get DMD guessing which namespaces to
- Walter (5/7) Aug 13 2004 Making this work would break the bottom-up typing system of D expression...
- Russ Lewis (2/11) Aug 13 2004 Why is this any harder than finding the names of things in imported file...
- Russ Lewis (15/31) Aug 13 2004 Ok, so to be just a little wacky, here's a enum-as-mixin
- Walter (8/19) Aug 14 2004 Enum
- Sean Kelly (6/10) Aug 14 2004 Always seemed pretty straightforward to me. And we're stuck with import
- Nick (5/11) Aug 14 2004 I say go for it! The import rules _are_ straightforward: if the identifi...
-
antiAlias
(9/21)
Aug 14 2004
- Ilya Minkov (4/7) Aug 15 2004 Ur, what?
- J C Calvarese (8/20) Aug 15 2004 If you can't guess who he is, he shouldn't have to bother telling you.
-
antiAlias
(13/16)
Aug 15 2004
- Arcane Jill (6/9) Aug 15 2004 I agree with this, and I'd just like to remind everyone that the big W h...
- Ilya Minkov (9/13) Aug 18 2004 Speaking of etiquette and such, it is usual that a person introduces
- Ilya Minkov (22/31) Aug 18 2004 I did take a short glance at them, but perhaps i missed the core. It is
- antiAlias (7/15) Aug 18 2004 Now there's a 'spin' if ever I saw one. I'll do you a favour Ilya:
- Ilya Minkov (6/12) Aug 19 2004 Pfft. And you are perfect and everyone should be like you. Omniscient
- Matthew (6/18) Aug 19 2004 Guys, guys. Please desist.
- Antti =?iso-8859-1?Q?Syk=E4ri?= (26/34) Oct 27 2004 I don't know if there was any kind of conclusion since I
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (20/47) Oct 28 2004 Here's the current dirty workaround that I resorted to:
- Antti =?iso-8859-1?Q?Syk=E4ri?= (19/27) Oct 28 2004 [...]
I have mentioned this before, but I kinda want some more opinions on the matter/idea. Currently we have to specify an enum by it's full name, this can be quite long and you can end up writing/copying/pasting the enum name a few times. Surely it's possible in most cases for the compiler to determine the Enum name and thus it's not really required. Example: enum FooBarBaz : ushort { THE = 0x0001, QUICK = 0x0002, BROWN = 0x0004, FOX = 0x0008, JUMPS = 0x0010, OVER = 0x0020, THE = 0x0040, LAZY = 0x0080, DOG = 0x0100 } void fooBar(FooBarBaz a) {} void main() { //current method fooBar(FooBarBaz.THE|FooBarBaz.QUICK|FooBarBaz.BROWN|FooBarBaz.FOX|FooBarBaz.JUMP|FooBarBaz.OVER|FooBarBaz.THE|FooBarBaz.LAZY|FooBarBaz.DOG); //proposed method fooBar(THE|QUICK|BROWN|FOX|JUMP|OVER|THE|LAZY|DOG); //alternate method fooBar(with(FooBarBaz){THE|QUICK|BROWN|FOX|JUMP|OVER|THE|LAZY|DOG}); } Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 09 2004
"Regan Heath" <regan netwin.co.nz> escribió en el mensaje news:opschrxff75a2sq9 digitalmars.com | enum FooBarBaz : ushort | { | THE = 0x0001, | QUICK = 0x0002, | BROWN = 0x0004, | FOX = 0x0008, | JUMPS = 0x0010, | OVER = 0x0020, | THE = 0x0040, | LAZY = 0x0080, | DOG = 0x0100 | } | You can do it like this: typedef ushort FooBarBaz; enum : FooBarBaz { ... } ----------------------- Carlos Santander Bernal
Aug 09 2004
or you can do it like this. enum : ushort { THE=0x01; ... } only problem is that you lose the enum name, which is nice to have if you have conflicting names (but that shouldn't happen very often).
Aug 09 2004
On Mon, 9 Aug 2004 21:49:06 -0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:or you can do it like this. enum : ushort { THE=0x01; ... } only problem is that you lose the enum name, which is nice to have if you have conflicting names (but that shouldn't happen very often).Thanks, I didn't realise you could have un-named enums. However I still think there is merit in my idea, Walter how hard would it be to have the compiler treat this: fooBar(THE); the same as fooBar(EnumName.THE); given that void fooBar(EnumName a); basically tells you what to expect. ? Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 09 2004
Regan Heath wrote:On Mon, 9 Aug 2004 21:49:06 -0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:The reason that it requires a fully qualified name is to prevent name clashes, and to make the code more clear. What would the compiler do if you had: void fooBar(ushort s); enum Foo : ushort { one, is, the, first, number } enum Bar : ushort { just, one, more, time } fooBar(one); // would it be Foo.one or Bar.one? Requiring Foo.one or Bar.one provides clearer code. If you just want global constants, then use an anonymous enum, that's what it's there for. -Dejaor you can do it like this. enum : ushort { THE=0x01; ... } only problem is that you lose the enum name, which is nice to have if you have conflicting names (but that shouldn't happen very often).Thanks, I didn't realise you could have un-named enums. However I still think there is merit in my idea, Walter how hard would it be to have the compiler treat this: fooBar(THE); the same as fooBar(EnumName.THE); given that void fooBar(EnumName a); basically tells you what to expect. ? Regan
Aug 09 2004
On Mon, 09 Aug 2004 21:57:03 -0500, Deja Augustine <deja scratch-ware.net> wrote:Regan Heath wrote:An excellent question. The answer, this requires a fully qualified name.On Mon, 9 Aug 2004 21:49:06 -0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:The reason that it requires a fully qualified name is to prevent name clashes, and to make the code more clear. What would the compiler do if you had: void fooBar(ushort s); enum Foo : ushort { one, is, the, first, number } enum Bar : ushort { just, one, more, time } fooBar(one); // would it be Foo.one or Bar.one?or you can do it like this. enum : ushort { THE=0x01; ... } only problem is that you lose the enum name, which is nice to have if you have conflicting names (but that shouldn't happen very often).Thanks, I didn't realise you could have un-named enums. However I still think there is merit in my idea, Walter how hard would it be to have the compiler treat this: fooBar(THE); the same as fooBar(EnumName.THE); given that void fooBar(EnumName a); basically tells you what to expect. ? ReganRequiring Foo.one or Bar.one provides clearer code.I agree that in your above example the fully qualified name makes it clearer, but then I'm not asking to be able to use non-fully qualified names in situations shown by your example. I don't believe fully qualifying makes _my_ example any clearer, nor does it make other similar situations any clearer.If you just want global constants, then use an anonymous enum, that's what it's there for.I don't want global constants specifically. I just don't want to have to type FooBarBaz 6 times. I'm not suggesting abolishing fully qualified names, far from it. I'm simply saying in certain situations it knows what you mean without you having to say so. (several times) Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 09 2004
Regan Heath wrote:On Mon, 09 Aug 2004 21:57:03 -0500, Deja Augustine <deja scratch-ware.net> wrote:Well, personally, I think that Walter has far far more important issues than reducing the amount of copy/paste you have to do because you want fully-qualified enums without using fully-qualified identifiers. The option of global constants exists for you if you're that lazy that you can't type an additional 10 characters to use an enum value. If you really are that lazy, just enclose the bit you want with: with(FooBarBaz) { /+ all your code you don't want to use an FQI for +/ } You've wasted more keystrokes typing up and defending this ridiculous request to assist lazy coding practices than you probably will use typing in "FooBarBaz." in your current project. I can see no justification for coding implicit enum resolution into the compiler when there are several much less ambiguous options available. -DejaRegan Heath wrote:An excellent question. The answer, this requires a fully qualified name.On Mon, 9 Aug 2004 21:49:06 -0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:The reason that it requires a fully qualified name is to prevent name clashes, and to make the code more clear. What would the compiler do if you had: void fooBar(ushort s); enum Foo : ushort { one, is, the, first, number } enum Bar : ushort { just, one, more, time } fooBar(one); // would it be Foo.one or Bar.one?or you can do it like this. enum : ushort { THE=0x01; ... } only problem is that you lose the enum name, which is nice to have if you have conflicting names (but that shouldn't happen very often).Thanks, I didn't realise you could have un-named enums. However I still think there is merit in my idea, Walter how hard would it be to have the compiler treat this: fooBar(THE); the same as fooBar(EnumName.THE); given that void fooBar(EnumName a); basically tells you what to expect. ? ReganRequiring Foo.one or Bar.one provides clearer code.I agree that in your above example the fully qualified name makes it clearer, but then I'm not asking to be able to use non-fully qualified names in situations shown by your example. I don't believe fully qualifying makes _my_ example any clearer, nor does it make other similar situations any clearer.If you just want global constants, then use an anonymous enum, that's what it's there for.I don't want global constants specifically. I just don't want to have to type FooBarBaz 6 times. I'm not suggesting abolishing fully qualified names, far from it. I'm simply saying in certain situations it knows what you mean without you having to say so. (several times) Regan
Aug 09 2004
Deja Augustine wrote:Regan Heath wrote:<snip>On Mon, 09 Aug 2004 21:57:03 -0500, Deja Augustine <deja scratch-ware.net> wrote:Regan Heath wrote:An excellent question. The answer, this requires a fully qualified name.On Mon, 9 Aug 2004 21:49:06 -0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:The reason that it requires a fully qualified name is to prevent name clashes, and to make the code more clear. What would the compiler do if you had: void fooBar(ushort s); enum Foo : ushort { one, is, the, first, number } enum Bar : ushort { just, one, more, time } fooBar(one); // would it be Foo.one or Bar.one?or you can do it like this. enum : ushort { THE=0x01; ... } only problem is that you lose the enum name, which is nice to have if you have conflicting names (but that shouldn't happen very often).Thanks, I didn't realise you could have un-named enums. However I still think there is merit in my idea, Walter how hard would it be to have the compiler treat this: fooBar(THE); the same as fooBar(EnumName.THE); given that void fooBar(EnumName a); basically tells you what to expect. ? ReganRequiring Foo.one or Bar.one provides clearer code.I agree that in your above example the fully qualified name makes it clearer, but then I'm not asking to be able to use non-fully qualified names in situations shown by your example. I don't believe fully qualifying makes _my_ example any clearer, nor does it make other similar situations any clearer.If you just want global constants, then use an anonymous enum, that's what it's there for.I don't want global constants specifically. I just don't want to have to type FooBarBaz 6 times. I'm not suggesting abolishing fully qualified names, far from it. I'm simply saying in certain situations it knows what you mean without you having to say so. (several times) ReganIf you really are that lazy, just enclose the bit you want with: with(FooBarBaz) { /+ all your code you don't want to use an FQI for +/ }<snip> I do stand corrected. with statements only work on classes and structs. -Deja
Aug 09 2004
On Tue, 10 Aug 2004 00:28:55 -0500, Deja Augustine <deja scratch-ware.net> wrote:Regan Heath wrote:I agree completely. I am not suggesting this be done 'right now' if you had read my initial post you would notice I said ".. I kinda want some more opinions on the matter/idea."On Mon, 09 Aug 2004 21:57:03 -0500, Deja Augustine <deja scratch-ware.net> wrote:Well, personally, I think that Walter has far far more important issues than reducing the amount of copy/paste you have to doRegan Heath wrote:An excellent question. The answer, this requires a fully qualified name.On Mon, 9 Aug 2004 21:49:06 -0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:The reason that it requires a fully qualified name is to prevent name clashes, and to make the code more clear. What would the compiler do if you had: void fooBar(ushort s); enum Foo : ushort { one, is, the, first, number } enum Bar : ushort { just, one, more, time } fooBar(one); // would it be Foo.one or Bar.one?or you can do it like this. enum : ushort { THE=0x01; ... } only problem is that you lose the enum name, which is nice to have if you have conflicting names (but that shouldn't happen very often).Thanks, I didn't realise you could have un-named enums. However I still think there is merit in my idea, Walter how hard would it be to have the compiler treat this: fooBar(THE); the same as fooBar(EnumName.THE); given that void fooBar(EnumName a); basically tells you what to expect. ? ReganRequiring Foo.one or Bar.one provides clearer code.I agree that in your above example the fully qualified name makes it clearer, but then I'm not asking to be able to use non-fully qualified names in situations shown by your example. I don't believe fully qualifying makes _my_ example any clearer, nor does it make other similar situations any clearer.If you just want global constants, then use an anonymous enum, that's what it's there for.I don't want global constants specifically. I just don't want to have to type FooBarBaz 6 times. I'm not suggesting abolishing fully qualified names, far from it. I'm simply saying in certain situations it knows what you mean without you having to say so. (several times) Reganbecause you want fully-qualified enums without using fully-qualified identifiers. The option of global constants exists for you if you're that lazy that you can't type an additional 10 characters to use an enum value. If you really are that lazy, just enclose the bit you want with: with(FooBarBaz) { /+ all your code you don't want to use an FQI for +/ } You've wasted more keystrokes typing up and defending this ridiculous request to assist lazy coding practices than you probably will use typing in "FooBarBaz." in your current project. I can see no justification for coding implicit enum resolution into the compiler when there are several much less ambiguous options available.Firstly my initial suggestion is not ambiguous, your example, which was not what I was suggesting was ambiguous. To explain, my idea: enum FooBarBaz { ONE,TWO } fooBar(ONE); is not any more/less ambiguous than fooBar(FooBarBaz.ONE); because if you're wondering what ONE is, and what it means to FooBar you have to lookup both the 'fooBar' function and the 'FooBarBaz' enum. In the case of my example, the former gives you the name of the latter. Secondly, the point of this thread is to hear people opinions, not to prompt Walter into action, so, while I thank you for your opinions I take exception to being called "lazy" repeatedly, and for no reason except that you have no real argument against my idea. Sure it's not a super important feature/idea, but it's a nice touch, one that so far you have given no real reasons against it, all you have said is: - that I am lazy - that walter has more important thing to do - that it is ambiguous. to all of which I believe I have replied in a civil manner, which is more than I can say for you. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 10 2004
In article <opscjg92ma5a2sq9 digitalmars.com>, Regan Heath says...On Tue, 10 Aug 2004 00:28:55 -0500, Deja Augustine <deja scratch-ware.net> wrote:But if the 'FooBarBaz' is omitted then the user does not know which enum to check. This is especially true if fooBar takes an int value and the enum is being implicitly cast. IMO D corrects what can be a very subtle source of bugs in C/C++ by requiring named enums to be qualified appropriately. I would very much not like this to change, especially if the reason is just to save a few keystrokes.I can see no justification for coding implicit enum resolution into the compiler when there are several much less ambiguous options available.Firstly my initial suggestion is not ambiguous, your example, which was not what I was suggesting was ambiguous. To explain, my idea: enum FooBarBaz { ONE,TWO } fooBar(ONE); is not any more/less ambiguous than fooBar(FooBarBaz.ONE); because if you're wondering what ONE is, and what it means to FooBar you have to lookup both the 'fooBar' function and the 'FooBarBaz' enum. In the case of my example, the former gives you the name of the latter.Sure it's not a super important feature/idea, but it's a nice touch, one that so far you have given no real reasons against it, all you have said is: - that I am lazy - that walter has more important thing to do - that it is ambiguous.The only point I agree with is the one regarding ambiguity. Consider this code: The above is unambiguous because there is only one defined value for RED. However, assume that during the course of maintenance a programmer discovers that he needs something from module b, so he imports that and for some reason removes the line importing module a. Everything compiles just fine because RED is still defined, but the PaintScreen call is now operating on a completely different value. In more complex cases such bugs can be extremely difficult to diagnose, especially if the affected code is something that has been left unchanged for years. Sean
Aug 11 2004
On Wed, 11 Aug 2004 21:23:57 +0000 (UTC), Sean Kelly <sean f4.ca> wrote:In article <opscjg92ma5a2sq9 digitalmars.com>, Regan Heath says...Yes they do.. think about how you'd go about it, first you'd look up the function, because what point is knowing the value of the enum if you don't know what's being done with it. The function includes the name of the enum... well... until I read your statement below I assumed it would :)On Tue, 10 Aug 2004 00:28:55 -0500, Deja Augustine <deja scratch-ware.net> wrote:But if the 'FooBarBaz' is omitted then the user does not know which enum to check.I can see no justification for coding implicit enum resolution into the compiler when there are several much less ambiguous options available.Firstly my initial suggestion is not ambiguous, your example, which was not what I was suggesting was ambiguous. To explain, my idea: enum FooBarBaz { ONE,TWO } fooBar(ONE); is not any more/less ambiguous than fooBar(FooBarBaz.ONE); because if you're wondering what ONE is, and what it means to FooBar you have to lookup both the 'fooBar' function and the 'FooBarBaz' enum. In the case of my example, the former gives you the name of the latter.This is especially true if fooBar takes an int value and the enum is being implicitly cast.Ahh.. I didn't think of this. :) So.. 'enum' is implicitly convertable to 'int' but 'int' is not implicitly convertable to 'enum', is that completely sensible do you think? I guess I assumed as 'int' wasn't implicitly convertable to 'enum' that the reverse would hold true also. I have just discovered something slightly weird... enum Test : ubyte { ONE = 0x1, TWO = 0x2 } void foo(Test a) { } void main() { foo(Test.ONE); foo(Test.ONE|Test.TWO); //function foo (Test a) does not match argument types (int) //cannot implicitly convert expression cast(int)(1) // | cast(int)(2) of type int to Test } Bug? or not?IMO D corrects what can be a very subtle source of bugs in C/C++ by requiring named enums to be qualified appropriately. I would very much not like this to change, especially if the reason is just to save a few keystrokes.The reason above is the first good one I've heard against my proposal. It probably kills it stone dead too.I assume you're missing an 'import b'; above?Sure it's not a super important feature/idea, but it's a nice touch, one that so far you have given no real reasons against it, all you have said is: - that I am lazy - that walter has more important thing to do - that it is ambiguous.The only point I agree with is the one regarding ambiguity. Consider this code: The above is unambiguous because there is only one defined value for RED.However, assume that during the course of maintenance a programmer discovers that he needs something from module b, so he imports that and for some reason removes the line importing module a. Everything compiles just fine because RED is still defined, but the PaintScreen call is now operating on a completely different value. In more complex cases such bugs can be extremely difficult to diagnose, especially if the affected code is something that has been left unchanged for years.Yeah.. my assumption about the implicit conversion was the killer. Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 11 2004
In article <opscld1nzg5a2sq9 digitalmars.com>, Regan Heath says...Yes you did. Or at least, your original idea, as I now understand it, completely covers this possibility. It works like this - assume your enum-type-detection system is up and running, then: This is simply a compile error, because f takes an int, and therefore "with Qwerty" is not assumed when parsing f(ONE); Accordingly, ONE is simply an unknown identifier. To get it to compile, you'd have either to change line 3 to f(Qwerty.ONE); or else change line 2 to void f(Qwerty x);This is especially true if fooBar takes an int value and the enum is being implicitly cast.Ahh.. I didn't think of this. :)I have just discovered something slightly weird... enum Test : ubyte { ONE = 0x1, TWO = 0x2 } void foo(Test a) { } void main() { foo(Test.ONE); foo(Test.ONE|Test.TWO); //function foo (Test a) does not match argument types (int) //cannot implicitly convert expression cast(int)(1) // | cast(int)(2) of type int to Test } Bug? or not?I think this is what I tried to mention in my last responce. The type of Test.ONE is Test, but the operator | takes ints, and returns an int, so both Test.ONE and Test.TWO are converted to int for the benefit of operator |, and the result is syntactically an int. Since ints cannot be implicitly cast to named enums, you have a compile error. You make it compile by doing: I have to say, though, I'm not greatly in favor of using enums as bit masks. I realize that (other) people do it all the time, but it seems to me that using an enum variable to hold a numerical value for which there is no corresponding enum name is violating (my conception of) what an enum is for. But that's just me, and I know other people do things differently.I've argued that it doesn't, however, /this/ might do: The evaluation of the expression f(ONE) requires an implicit "with", which the compiler can only get from the declaration of f() - but this requires knowing in advance which overload of f() is going to be used. Unfortunately, overload resolution requires knowing the types of the arguments, and so requires the arguments to be parsed /first/. We have ourselves some circularity! The only way out of this would be to allow "implicit withs" only on non-overloaded functions. (Or at least, functions which are not overloaded to the point of ambiguity, but we may be asking too much of the compiler at this point). Arcane JillIMO D corrects what can be a very subtle source of bugs in C/C++ by requiring named enums to be qualified appropriately. I would very much not like this to change, especially if the reason is just to save a few keystrokes.The reason above is the first good one I've heard against my proposal. It probably kills it stone dead too.
Aug 11 2004
On Thu, 12 Aug 2004 06:56:43 +0000 (UTC), Arcane Jill <Arcane_member pathlink.com> wrote:In article <opscld1nzg5a2sq9 digitalmars.com>, Regan Heath says...True..Yes you did. Or at least, your original idea, as I now understand it, completely covers this possibility. It works like this - assume your enum-type-detection system is up and running, then: This is simply a compile error, because f takes an int, and therefore "with Qwerty" is not assumed when parsing f(ONE); Accordingly, ONE is simply an unknown identifier. To get it to compile, you'd have either to change line 3 to f(Qwerty.ONE); or else change line 2 to void f(Qwerty x);This is especially true if fooBar takes an int value and the enum is being implicitly cast.Ahh.. I didn't think of this. :)Not quite.. as this works: enum EnumName { A,B,C }; void foo(EnumName a); foo(EnumName.A|EnumName.B|EnumName.C); the problem with the above is the ": ubyte" on the enum.I have just discovered something slightly weird... enum Test : ubyte { ONE = 0x1, TWO = 0x2 } void foo(Test a) { } void main() { foo(Test.ONE); foo(Test.ONE|Test.TWO); //function foo (Test a) does not match argument types (int) //cannot implicitly convert expression cast(int)(1) // | cast(int)(2) of type int to Test } Bug? or not?I think this is what I tried to mention in my last responce.The type of Test.ONE is Test, but the operator | takes ints, and returns an int, so both Test.ONE and Test.TWO are converted to int for the benefit of operator |, and the result is syntactically an int. Since ints cannot be implicitly cast to named enums, you have a compile error.It appears something like this is heppening, but something else is also going on when the enum has no base type specified, it seems the 'int' produced by the | _can_ be implicitly cast back to the enum type.You make it compile by doing: I have to say, though, I'm not greatly in favor of using enums as bit masks. I realize that (other) people do it all the time, but it seems to me that using an enum variable to hold a numerical value for which there is no corresponding enum name is violating (my conception of) what an enum is for. But that's just me, and I know other people do things differently.So how do you define a bunch of flag (off/on) options which you are storing in a ubyte, uint or ulong?Yeah, that is nasty, I think in this case and any other where it is ambiguous you simply have to provide the enum ('A' or 'B' in the above example). Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/I've argued that it doesn't, however, /this/ might do: The evaluation of the expression f(ONE) requires an implicit "with", which the compiler can only get from the declaration of f() - but this requires knowing in advance which overload of f() is going to be used. Unfortunately, overload resolution requires knowing the types of the arguments, and so requires the arguments to be parsed /first/. We have ourselves some circularity! The only way out of this would be to allow "implicit withs" only on non-overloaded functions. (Or at least, functions which are not overloaded to the point of ambiguity, but we may be asking too much of the compiler at this point).IMO D corrects what can be a very subtle source of bugs in C/C++ by requiring named enums to be qualified appropriately. I would very much not like this to change, especially if the reason is just to save a few keystrokes.The reason above is the first good one I've heard against my proposal. It probably kills it stone dead too.
Aug 16 2004
In article <opscuuezyz5a2sq9 digitalmars.com>, Regan Heath says...Me, personally? Well, in C and C++, I would do this: But of course you can't do that in D. In other langauges, I might do something like: although of course the code is then less typesafe. More likely, I'd just do: (although that, of course, is not bit-packed). In D, however, there is a brand new option, to replace C's struct bitfields, which is this: I've just started playing with this idea. I have no idea how efficient it is/isn't. JillI have to say, though, I'm not greatly in favor of using enums as bit masks. I realize that (other) people do it all the time, but it seems to me that using an enum variable to hold a numerical value for which there is no corresponding enum name is violating (my conception of) what an enum is for. But that's just me, and I know other people do things differently.So how do you define a bunch of flag (off/on) options which you are storing in a ubyte, uint or ulong?
Aug 16 2004
On Tue, 17 Aug 2004 06:55:26 +0000 (UTC), Arcane Jill <Arcane_member pathlink.com> wrote:In D, however, there is a brand new option, to replace C's struct bitfields, which is this: I've just started playing with this idea. I have no idea how efficient it is/isn't.But of course! I simply wasn't thinking in D! Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 17 2004
"Jarrett Billingsley" <kb3ctd2 yahoo.com> escribió en el mensaje news:cf99h1$160a$1 digitaldaemon.com | or you can do it like this. | | enum : ushort | { | THE=0x01; | ... | } | | only problem is that you lose the enum name, which is nice to have if you | have conflicting names (but that shouldn't happen very often). But then he loses his FooBarBaz type. ----------------------- Carlos Santander Bernal
Aug 11 2004
In article <opschrxff75a2sq9 digitalmars.com>, Regan Heath says...//proposed method fooBar(THE|QUICK|BROWN|FOX|JUMPS|OVER|THE|LAZY|DOG);Would that work? I mean, I can see that: would work, since (assuming your proposed scheme) the compiler would be able to infer the type of the fooBar parameter. But as for I have to ask - what is the compile-time type of the expression: My guess is that the compile-time type of even the expression: FooBarBaz.THE|FooBarBaz.QUICK|FooBarBaz.BROWN|FooBarBaz.FOX|FooBarBaz.JUMP|FooBarBaz.OVER|FooBarBaz.THE|FooBarBaz.LAZY|FooBarBaz.DOG is not actually FooBarBaz, but is in fact int. Does the | operator preserve the type of an enum? I'm not sure that it does. Arcane Jill (basically in support but with one or two niggling doubts).
Aug 10 2004
On Tue, 10 Aug 2004 11:56:06 +0000 (UTC), Arcane Jill <Arcane_member pathlink.com> wrote:In article <opschrxff75a2sq9 digitalmars.com>, Regan Heath says...Good question.. I think Walter is the only one who can really answer it, however, my tests show that you cannot pass an 'int' to a function expecting a 'FooBarBaz', but you can pass something like 'FooBarBaz.ONE|FooBarBaz.TWO'. In fact you can also cast and pass another enum type if you want. enum FooBarBaz { ONE = 0x1, TWO = 0x2, THREE = 0x4, FOUR = 0x8 } enum BarBazFoo { ONE = 0x8, TWO = 0x4, THREE = 0x2, FOUR = 0x1 } void fooBar(FooBarBaz a) { printf("%x\n",a); } void main() { int i = FooBarBaz.ONE|FooBarBaz.TWO; fooBar(i); //function fooBar (FooBarBaz a) does not match argument types (int) //cannot implicitly convert expression i of type int to FooBarBaz fooBar(FooBarBaz.ONE|FooBarBaz.TWO); fooBar(cast(FooBarBaz)BarBazFoo.ONE|BarBazFoo.TWO); }//proposed method fooBar(THE|QUICK|BROWN|FOX|JUMPS|OVER|THE|LAZY|DOG);Would that work? I mean, I can see that: would work, since (assuming your proposed scheme) the compiler would be able to infer the type of the fooBar parameter. But as for I have to ask - what is the compile-time type of the expression: My guess is that the compile-time type of even the expression: FooBarBaz.THE|FooBarBaz.QUICK|FooBarBaz.BROWN|FooBarBaz.FOX|FooBarBaz.JUMP|FooBarBaz.OVER|FooBarBaz.THE|FooBarBaz.LAZY|FooBarBaz.DOG is not actually FooBarBaz, but is in fact int. Does the | operator preserve the type of an enum? I'm not sure that it does.Arcane Jill (basically in support but with one or two niggling doubts).Glad to see someone is thinking about how genreally 'nice' this would be without accusing me of being lazy. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 10 2004
In article <opscjhxy015a2sq9 digitalmars.com>, Regan Heath says...Glad to see someone is thinking about how genreally 'nice' this would be without accusing me of being lazy.I take pride in being lazy. When I was studying math at school, I was taught that math is all about being lazy, and that the very best mathematicians are the laziest of all. If you cou prove a theorem ONCE for (say) "rings", then you've proven it for reals, integers, whatever. There are parallels with OOP here - component re-use is all about being lazy. I also write libraries because I'm lazy (why write the same piece of code more than once?). I use Phobos routines because I'm lazy (why write a piece of code at all if someone else has done it for me?). I even write in D because I'm lazy (why invent my own brand new computer language when I can just use Walter's?). I dunno, but I guess those mad workaholic types just enjoy re-inventing the wheel all the time, or maybe they just get a buzz out of pressing keys on their keyboard? Jill (Lazy and proud of it)
Aug 11 2004
In article <cfcgjg$4v6$1 digitaldaemon.com>, Arcane Jill says...In article <opscjhxy015a2sq9 digitalmars.com>, Regan Heath says...and of course "efficience is the smart lazy" (better wording welcome) AntGlad to see someone is thinking about how genreally 'nice' this would be without accusing me of being lazy.I take pride in being lazy.
Aug 11 2004
Give the hardest job to the laziest person and they will find the quickest way of doing it. On Wed, 11 Aug 2004 12:30:56 +0000 (UTC) Ant <Ant_member pathlink.com> wrote:In article <cfcgjg$4v6$1 digitaldaemon.com>, Arcane Jill says...In article <opscjhxy015a2sq9 digitalmars.com>, Regan Heath says...and of course "efficience is the smart lazy" (better wording welcome) AntGlad to see someone is thinking about how genreally 'nice' this would be without accusing me of being lazy.I take pride in being lazy.
Aug 11 2004
"Arcane Jill" <Arcane_member pathlink.com> wrote in message news:cfcgjg$4v6$1 digitaldaemon.com...I take pride in being lazy. When I was studying math at school, I wastaughtthat math is all about being lazy, and that the very best mathematiciansare thelaziest of all. If you cou prove a theorem ONCE for (say) "rings", thenyou'veproven it for reals, integers, whatever. There are parallels with OOPhere -component re-use is all about being lazy. I also write libraries becauseI'mlazy (why write the same piece of code more than once?). I use Phobosroutinesbecause I'm lazy (why write a piece of code at all if someone else hasdone itfor me?). I even write in D because I'm lazy (why invent my own brand new computer language when I can just use Walter's?). I dunno, but I guessthose madworkaholic types just enjoy re-inventing the wheel all the time, or maybetheyjust get a buzz out of pressing keys on their keyboard?Most engineering progress is made by lazy people. The person who invented automatic valves on steam engines was the boy who had to run up and down a ladder for every stroke of the piston. He rigged up a pole and some levers to do it for him, and was discovered asleep beside the engine, which was running just fine.
Aug 13 2004
Regan Heath schrieb:I would like to silently thumb up the remark and ideas as well. -eyeArcane Jill (basically in support but with one or two niggling doubts).Glad to see someone is thinking about how genreally 'nice' this would be without accusing me of being lazy.
Aug 13 2004
Regan Heath wrote:I have mentioned this before, but I kinda want some more opinions on the matter/idea. Currently we have to specify an enum by it's full name, this can be quite long and you can end up writing/copying/pasting the enum name a few times. Surely it's possible in most cases for the compiler to determine the Enum name and thus it's not really required. Example: enum FooBarBaz : ushort { THE = 0x0001, QUICK = 0x0002, BROWN = 0x0004, FOX = 0x0008, JUMPS = 0x0010, OVER = 0x0020, THE = 0x0040, LAZY = 0x0080, DOG = 0x0100 } void fooBar(FooBarBaz a) {} void main() { //current method fooBar(FooBarBaz.THE|FooBarBaz.QUICK|FooBarBaz.BROWN|FooBarBaz.FOX|FooBarBaz.JUMP|FooBarBaz.OVER|FooBarBaz.THE|FooBarBaz. AZY|FooBarBaz.DOG); //proposed method fooBar(THE|QUICK|BROWN|FOX|JUMP|OVER|THE|LAZY|DOG); //alternate method fooBar(with(FooBarBaz){THE|QUICK|BROWN|FOX|JUMP|OVER|THE|LAZY|DOG}); }I don't think it's a good idea to get DMD guessing which namespaces to search when it can't find a symbol, nor does it feel like a particularly good idea to use the with() construct as an expression. What would be simpler, and about as useful, though, would be if with() were also usable as an attribute specifier, like public/private/etc. class Foo { // Java-style with(FooBarBaz) const FooBarBaz flags = THE | QUICK | BROWN | FOX; // C++-style with (FooBarBaz): const FooBarBaz flags = ...; } Incidently, it's becoming apparent that we have three different keywords for almost the same thing: with(), import, and alias. :) -- andy
Aug 11 2004
"Regan Heath" <regan netwin.co.nz> wrote in message news:opschrxff75a2sq9 digitalmars.com...Surely it's possible in most cases for the compiler to determine the Enum name and thus it's not really required.Making this work would break the bottom-up typing system of D expressions. It has the potential of making the types of many expressions simply unresolvable.
Aug 13 2004
Walter wrote:"Regan Heath" <regan netwin.co.nz> wrote in message news:opschrxff75a2sq9 digitalmars.com...Why is this any harder than finding the names of things in imported files?Surely it's possible in most cases for the compiler to determine the Enum name and thus it's not really required.Making this work would break the bottom-up typing system of D expressions. It has the potential of making the types of many expressions simply unresolvable.
Aug 13 2004
Russ Lewis wrote:Walter wrote:Ok, so to be just a little wacky, here's a enum-as-mixin enum Foo : uint { FOO, BAR, BAZ; }; turns into: template FooTemplate(T) { typedef T Foo; const T FOO = 0; const T BAR = 1; const T BAZ = 2; } mixin FooTemplate!(uint) FooM; void func(Foo f) { ... } func(FOO | BAR | BAZ); // if there is any ambiguity with other enums or local variables... func(FooM.FOO | FooM.BAR | FooM.BAZ);"Regan Heath" <regan netwin.co.nz> wrote in message news:opschrxff75a2sq9 digitalmars.com...Why is this any harder than finding the names of things in imported files?Surely it's possible in most cases for the compiler to determine the Enum name and thus it's not really required.Making this work would break the bottom-up typing system of D expressions. It has the potential of making the types of many expressions simply unresolvable.
Aug 13 2004
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:cfjlm3$1cr3$1 digitaldaemon.com...Walter wrote:Enum"Regan Heath" <regan netwin.co.nz> wrote in message news:opschrxff75a2sq9 digitalmars.com...Surely it's possible in most cases for the compiler to determine theexpressions.name and thus it's not really required.Making this work would break the bottom-up typing system of DI hadn't thought of implementing it like an import, you might be right. I think the semantics of looking up symbols in imports is straightforward, but a lot of people here find it incomprehensible. I don't understand why, but because of that I am hesitant to expand its application.It has the potential of making the types of many expressions simply unresolvable.Why is this any harder than finding the names of things in imported files?
Aug 14 2004
In article <cflk30$2io9$1 digitaldaemon.com>, Walter says...I hadn't thought of implementing it like an import, you might be right. I think the semantics of looking up symbols in imports is straightforward, but a lot of people here find it incomprehensible. I don't understand why, but because of that I am hesitant to expand its application.Always seemed pretty straightforward to me. And we're stuck with import behavior for import anyway, so why not expand it to cover other areas where it seems appropriate? It would be better than having different rules for different aspects of the language. Sean
Aug 14 2004
In article <cflk30$2io9$1 digitaldaemon.com>, Walter says...I say go for it! The import rules _are_ straightforward: if the identifier exist in one module, use it. If it exists in more modules, complain. Use alias to remove the complaints. That's it ;-) NickWhy is this any harder than finding the names of things in imported files?I hadn't thought of implementing it like an import, you might be right. I think the semantics of looking up symbols in imports is straightforward, but a lot of people here find it incomprehensible. I don't understand why, but because of that I am hesitant to expand its application.
Aug 14 2004
<groan> Please ... no more alias ... "Nick" <Nick_member pathlink.com> wrote in message news:cfln3a$2k0p$1 digitaldaemon.com...In article <cflk30$2io9$1 digitaldaemon.com>, Walter says...files?Why is this any harder than finding the names of things in importedbutI hadn't thought of implementing it like an import, you might be right. I think the semantics of looking up symbols in imports is straightforward,buta lot of people here find it incomprehensible. I don't understand why,existbecause of that I am hesitant to expand its application.I say go for it! The import rules _are_ straightforward: if the identifierin one module, use it. If it exists in more modules, complain. Use aliastoremove the complaints. That's it ;-) Nick
Aug 14 2004
antiAlias schrieb:<groan> Please ... no more alias ...Ur, what? Who are you anyway? -eye/photoallergics
Aug 15 2004
Ilya Minkov wrote:antiAlias schrieb:If you can't guess who he is, he shouldn't have to bother telling you. Think about it... Think about whose name might have disappeared about the same time the new name appeared. Read antiAlias's posts. It's not that hard to figure out.<groan> Please ... no more alias ...Ur, what? Who are you anyway?-eye/photoallergics-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Aug 15 2004
"Ilya Minkov" <minkov cs.tum.edu> wrote in message news:cfnkur$hqi$1 digitaldaemon.com...Ur, what? Who are you anyway? -eye/photoallergics<sigh> If you perhaps had a sense of joviality you would already know. Or, if you'd bother to look at prior posts, you would already know. My name is Kris, and, if you'll graciously concede to it, I'll post under whatever "alias" suits; just as others do. That aside, I'm certainly entitled to my opinion. Just as you, apparently, are entitled to your rather staunch opinion over a variety of matters. Further; if you had any sense of etiquette, you'd understand that it's considered rather 'uncool' to jostle individuals over their identity in a public forum. Or perhaps you were being pompously arrogant instead. I'll bid you "good-day"
Aug 15 2004
In article <cfoa52$unf$1 digitaldaemon.com>, antiAlias says...it's considered rather 'uncool' to jostle individuals over their identity in a public forum.I agree with this, and I'd just like to remind everyone that the big W himself (in post http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/3072) said: "in general I'd like to request that we leave it up to the person using a handle to reveal themselves or not as their choice". Jill
Aug 15 2004
Arcane Jill schrieb:I agree with this, and I'd just like to remind everyone that the big W himself (in post http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/3072) said: "in general I'd like to request that we leave it up to the person using a handle to reveal themselves or not as their choice".Speaking of etiquette and such, it is usual that a person introduces himself or herself before speaking up. It is up to the person to share or not any personal data, but some piece of relevant information such as "I am new to D" or "I have been using D for a while" or "I have some experience in this other language" or "I have worked in this-or-that field of interest" or somesuch is in fact necessary for successful communication. -eye
Aug 18 2004
antiAlias schrieb:If you perhaps had a sense of joviality you would already know. Or, if you'd bother to look at prior posts, you would already know. My name is Kris, and, if you'll graciously concede to it, I'll post under whatever "alias" suits; just as others do.I did take a short glance at them, but perhaps i missed the core. It is beyond my possibility to read everything on this newsgroup. And isn't it a bit arrogant of you to think that everyone should recognize you from one word? And though i do change my nicks from time to time, i have always posted under my real name.That aside, I'm certainly entitled to my opinion. Just as you, apparently, are entitled to your rather staunch opinion over a variety of matters.I don't have a strong opinion on most matters, but i tend to support Walter unless i have been convinced otherwise. And i'm easy to convince.Further; if you had any sense of etiquette, you'd understand that it's considered rather 'uncool' to jostle individuals over their identity in a public forum. Or perhaps you were being pompously arrogant instead.Yes, i am arrogant. But i'm nontheless sorry if this appeared hostile to you. But look at it from my point of view. If i had recognized that it comes from you, it would be totally different, because i have read a number of posts from you, which make me think you are a smart person. And though i may be not always of the same opinion as you, i have come to respect your opinion because it has ground, and it's explained well, and you have a major influence on my opinion. But the only thing i saw was someone i don't know just shouting out "BULLSHIT" without any explaination. So i just wanted to have that cleared up, whether the person has a reeason to say so. Under a certain kind of mood, i could have written a much more harsh message than i did if i had time. -eye
Aug 18 2004
"Ilya Minkov" <minkov cs.tum.edu> wrote ..beyond my possibility to read everything on this newsgroup. And isn't it a bit arrogant of you to think that everyone should recognize you from one word?Now there's a 'spin' if ever I saw one. I'll do you a favour Ilya: http://thesaurus.reference.com/search?q=pseudonym%20 . See those references to "alias", "incognito", "anonym" ...But look at it from my point of view. If i had recognized that it comes from you, it would be totally different,Ahh ... shallowness in its very finest guise.cleared up, whether the person has a reeason to say so. Under a certain kind of mood, i could have written a much more harsh message than i did if i had time.Thrilled to hear you're such a truly thoughtful, warm, and charming individual.
Aug 18 2004
antiAlias schrieb:Now there's a 'spin' if ever I saw one. I'll do you a favour Ilya: http://thesaurus.reference.com/search?q=pseudonym%20 . See those references to "alias", "incognito", "anonym" ...Should that add any value to the empty message of yours?Ahh ... shallowness in its very finest guise.Pfft. And you are perfect and everyone should be like you. Omniscient amd omnipotent.Thrilled to hear you're such a truly thoughtful, warm, and charming individual.Hrrr. Hr Hr Hr. :>>>>>> -eye
Aug 19 2004
Guys, guys. Please desist. It pains me to see two smart chaps, both of whom I respect, in a slagging match. Now if one of you were an idiot, I'd cheer insanely as I watched the unfair crushing of the Christian in the lion's jaws. Simon the Softie "Ilya Minkov" <minkov cs.tum.edu> wrote in message news:cg20pb$1qn9$1 digitaldaemon.com...antiAlias schrieb:Now there's a 'spin' if ever I saw one. I'll do you a favour Ilya: http://thesaurus.reference.com/search?q=pseudonym%20 . See those references to "alias", "incognito", "anonym" ...Should that add any value to the empty message of yours?Ahh ... shallowness in its very finest guise.Pfft. And you are perfect and everyone should be like you. Omniscient amd omnipotent.Thrilled to hear you're such a truly thoughtful, warm, and charming individual.Hrrr. Hr Hr Hr. :>>>>>> -eye
Aug 19 2004
Regan Heath:I have mentioned this before, but I kinda want some more opinions on the matter/idea. Currently we have to specify an enum by it's full name, this can be quite long and you can end up writing/copying/pasting the enum name a few times. Surely it's possible in most cases for the compiler to determine the Enum name and thus it's not really required.I don't know if there was any kind of conclusion since I skimmed this thread pretty quickly (several months of catching up to do), but is this a possibility: enum FooBarBaz : ushort { THE = 0x0001, QUICK = 0x0002, BROWN = 0x0004, /* and what have you */ } void fooBar(FooBarBaz a) { { FooBarBaz x = THE; // error - THE not in scope FooBarBaz y = FooBarBaz.THE // ok } { import FooBarBaz; // bring names in FooBarBaz in current scope FooBarBaz x = THE; } } As this has a direct analogy in importing modules, there is no new semantic baggage. And no need to complicate lookup rules, I believe. -Antti
Oct 27 2004
Antti Sykäri wrote:I don't know if there was any kind of conclusion since I skimmed this thread pretty quickly (several months of catching up to do), but is this a possibility: enum FooBarBaz : ushort { THE = 0x0001, QUICK = 0x0002, BROWN = 0x0004, /* and what have you */ } void fooBar(FooBarBaz a) { { FooBarBaz x = THE; // error - THE not in scope FooBarBaz y = FooBarBaz.THE // ok } { import FooBarBaz; // bring names in FooBarBaz in current scope FooBarBaz x = THE; } } As this has a direct analogy in importing modules, there is no new semantic baggage. And no need to complicate lookup rules, I believe.Here's the current dirty workaround that I resorted to: //enum FooBarBaz : ushort alias ushort FooBarBaz; enum { THE = 0x0001, QUICK = 0x0002, BROWN = 0x0004, /* and what have you */ } Not type-safe, but "better" than porting all that C... Some kind of import, like you suggest above, would be a nice feature and minimize typing, and similar hacks. Having to type the enum each time is about as tedious as typing the full module name for each function call. And "import" is a handy solution for that already, so... (and with the "alias" to resolve any naming collision) I couldn't find it in the FeatureRequestList, though ? --anders
Oct 28 2004
In article <clqc7n$1lf8$1 digitaldaemon.com>, Anders F Björklund wrote:Antti Sykäri wrote:[my suggestion to pull names out of enums with import]Here's the current dirty workaround that I resorted to:[...]Not type-safe, but "better" than porting all that C... Some kind of import, like you suggest above, would be a nice feature and minimize typing, and similar hacks.Yes. Better type-safe than sorry, right?. IMO language should not punish you for wanting type safety. "import" could be extended to other things than just enums - actually everything that contain a namespace. While it would be most useful with enums, occasionally I'd like to "import" some static members of classes/interfaces/structs and template instances as well.I couldn't find it in the FeatureRequestList, though ?So there was a list like that. Here's the URL to those that haven't found it yet: http://www.prowiki.org/wiki4d/wiki.cgi?FeatureRequestList Actually I went to wiki4d in the first place but couldn't find a link to anything like that, so I gave up. Back to the "import for enums, than modules": Perhaps I, or someone else, will need to write an entry in that list. Is there any interest for a feature like this? -Antti
Oct 28 2004