Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 11, 2013 enum editing | ||||
---|---|---|---|---|
| ||||
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? |
June 11, 2013 Re: enum editing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seany | 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
|
June 11, 2013 Re: enum editing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seany | 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 |
June 11, 2013 Re: enum editing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | 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?
|
June 11, 2013 Re: enum editing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seany | 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 |
June 11, 2013 Re: enum editing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | 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.
|
June 11, 2013 Re: enum editing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seany | 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 |
June 11, 2013 Re: enum editing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seany | 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 |
June 11, 2013 Re: enum editing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seany | 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?
|
June 12, 2013 Re: enum editing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seany | 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...
|
Copyright © 1999-2021 by the D Language Foundation