www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - enum editing

reply "Seany" <seany uni-bonn.de> writes:
Hello

i read here that enums, once initialized, are constants :
http://ddili.org/ders/d.en/enum.html

However, i need a method, if possible, to dynamically (cexecution 
time) definition of enums, and updating them.

anyidea how that can be done? using const and/or inout, and 
passing an array / tuple to a function?
Jun 11 2013
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 11 Jun 2013 15:52:00 -0400, Seany <seany uni-bonn.de> wrote:

 Hello

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html

 However, i need a method, if possible, to dynamically (cexecution time)  
 definition of enums, and updating them.
No, use a variable. -Steve
Jun 11 2013
prev sibling next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2013-06-11, 21:52, Seany wrote:

 Hello

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html

 However, i need a method, if possible, to dynamically (cexecution time)  
 definition of enums, and updating them.

 anyidea how that can be done? using const and/or inout, and passing an  
 array / tuple to a function?
So you're saying you need something like this? struct MyEnum { int value; static MyEnum a(0); static MyEnum b(1); } void foo( ) { MyEnum.b = MyEnum(4); } -- Simen
Jun 11 2013
parent reply "Seany" <seany uni-bonn.de> writes:
On Tuesday, 11 June 2013 at 20:00:23 UTC, Simen Kjaeraas wrote:
 On 2013-06-11, 21:52, Seany wrote:

 Hello

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html

 However, i need a method, if possible, to dynamically 
 (cexecution time) definition of enums, and updating them.

 anyidea how that can be done? using const and/or inout, and 
 passing an array / tuple to a function?
So you're saying you need something like this? struct MyEnum { int value; static MyEnum a(0); static MyEnum b(1); } void foo( ) { MyEnum.b = MyEnum(4); }
thank you, but that means that when i need this to behave as const, i should use proper keywords ya?
Jun 11 2013
parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2013-06-11, 22:15, Seany wrote:

 On Tuesday, 11 June 2013 at 20:00:23 UTC, Simen Kjaeraas wrote:
 On 2013-06-11, 21:52, Seany wrote:

 Hello

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html

 However, i need a method, if possible, to dynamically (cexecution  
 time) definition of enums, and updating them.

 anyidea how that can be done? using const and/or inout, and passing an  
 array / tuple to a function?
So you're saying you need something like this? struct MyEnum { int value; static MyEnum a(0); static MyEnum b(1); } void foo( ) { MyEnum.b = MyEnum(4); }
thank you, but that means that when i need this to behave as const, i should use proper keywords ya?
I'm not sure I follow. (I must admit I find this whole discussion a bit weird, as we're basically just making glorified global variables) When do you want it to behave as const? When being used as a local variable? void foo() { const MyEnum var = MyEnum.a; } Or are you asking for MyEnum.a to be const except under certain circumstances? If so, how, why, when and are you sure that makes sense? -- Simen
Jun 11 2013
prev sibling next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2013-06-11, 21:52, Seany wrote:

 Hello

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html

 However, i need a method, if possible, to dynamically (cexecution time)  
 definition of enums, and updating them.

 anyidea how that can be done? using const and/or inout, and passing an  
 array / tuple to a function?
Expounding on my previous post: So you need something like this: int i; -- Simen
Jun 11 2013
parent reply "Seany" <seany uni-bonn.de> writes:
On Tuesday, 11 June 2013 at 20:18:16 UTC, Simen Kjaeraas wrote:
 On 2013-06-11, 21:52, Seany wrote:

 Hello

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html

 However, i need a method, if possible, to dynamically 
 (cexecution time) definition of enums, and updating them.

 anyidea how that can be done? using const and/or inout, and 
 passing an array / tuple to a function?
Expounding on my previous post: So you need something like this: int i;
no, i would like to define a range as a DISCREET domain / image of a function, with the possibility of modifying it when exclusively commanded.
Jun 11 2013
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 11 June 2013 at 20:49:58 UTC, Seany wrote:
 On Tuesday, 11 June 2013 at 20:18:16 UTC, Simen Kjaeraas wrote:
 On 2013-06-11, 21:52, Seany wrote:

 Hello

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html

 However, i need a method, if possible, to dynamically 
 (cexecution time) definition of enums, and updating them.

 anyidea how that can be done? using const and/or inout, and 
 passing an array / tuple to a function?
Expounding on my previous post: So you need something like this: int i;
no, i would like to define a range as a DISCREET domain / image of a function, with the possibility of modifying it when exclusively commanded.
I'm not following you. Could you give a clearer explanation of what you're trying to achieve?
Jun 11 2013
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/11/2013 12:52 PM, Seany wrote:

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html
enum is a type definition with a limited set of values. That part cannot be changed at runtime. (A type is a compile-time concept in D.) Of course then there are variables of an enum. Their values can change but they can take only those limited set of values. (A cast can be used to have invalid enum values. Not recommended. ;))
 However, i need a method, if possible, to dynamically (cexecution time)
 definition of enums, and updating them.
If you are talking about changing the set of values, then it is not possible with enums at runtime. You must represent that "type" some other way. For example, you can have an associative array that you can add values to. Ali
Jun 11 2013
parent "seany" <seany uni-bonn.de> writes:
On Tuesday, 11 June 2013 at 21:02:55 UTC, Ali Çehreli wrote:
 On 06/11/2013 12:52 PM, Seany wrote:

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html
enum is a type definition with a limited set of values. That part cannot be changed at runtime. (A type is a compile-time concept in D.) Of course then there are variables of an enum. Their values can change but they can take only those limited set of values. (A cast can be used to have invalid enum values. Not recommended. ;))
 However, i need a method, if possible, to dynamically
(cexecution time)
 definition of enums, and updating them.
If you are talking about changing the set of values, then it is not possible with enums at runtime. You must represent that "type" some other way. For example, you can have an associative array that you can add values to. Ali
aha. thank you. i was nt thinking in the line of "type of a variable" I was thinking of a function, whose domain and range is discreet and constant, example : a function that randomly generates a photon to hit and pass through and/or get absorved by a cloud (the cloud is discreetized, and the coordinates of the fgrd cells are constant) will deal with a constant, discreet domain / range. but i may want to move the cloud, or create turbulence in it. in those cases, i will need to redefine the cell coordinates. that is, i might want to exclusively redefine domain / range. now since this is a matter of type fixed at the compile time, i guess i will use good old arrays, and make them constant when needed.
Jun 13 2013
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 11 June 2013 at 19:52:01 UTC, Seany wrote:
 Hello

 i read here that enums, once initialized, are constants :
 http://ddili.org/ders/d.en/enum.html

 However, i need a method, if possible, to dynamically 
 (cexecution time) definition of enums, and updating them.

 anyidea how that can be done? using const and/or inout, and 
 passing an array / tuple to a function?
You could..., but it'd be nothing more than a glorified global struct. Inside a module, I'd put this: //---- private struct Enum { static { public property const { uint a(){return _a;} uint b(){return _b;} uint c(){return _c;} } private { uint _a = 0; uint _b = 1; uint _c = 2; } } } void changeState() { Enum._a = 2; Enum._c = 0; } //---- Then you can use it... void main() { writeln(Enum.a); writeln(Enum.c); changeState(); writeln(Enum.a); writeln(Enum.c); } But even then, keep i mind these aren't compile time objects, so you can't use them in a switch, for example...
Jun 12 2013
parent "seany" <seany uni-bonn.de> writes:
On Wednesday, 12 June 2013 at 09:13:20 UTC, monarch_dodra wrote:

 But even then, keep i mind these aren't compile time objects, 
 so you can't use them in a switch, for example...
thanks, i was going to commit precisely that sin.
Jun 13 2013