anyway to set a const object after the fact?

aliak something at something.com
Tue Oct 30 11:23:48 UTC 2018


On Tuesday, 30 October 2018 at 08:18:15 UTC, Laurent Tréguier 
wrote:
> On Monday, 29 October 2018 at 21:50:32 UTC, aliak wrote:
>> Hi, so if you have this piece of code:
>>
>> struct C {
>>
>>   void f() {
>>     string[] others;
>>     const string[] restArgs;
>>     foreach (i, arg; args) {
>>       if (isValidArg(arg)) {
>>         restArgs = args[i + 1 .. $];
>>         break;
>>       }
>>       others ~= arg;
>>     }
>>     // "others" is a list of args before the valid arg is 
>> encountered
>>     // "restArgs" is a list that is the args after the valid 
>> arg
>>   }
>> }
>>
>> Is there anyway to set a const object after declaring it in 
>> the above context?
>>
>> Cheers,
>> - Ali
>
> It looks like there is a Rebindable type for that in 
> std.typecons : 
> https://dlang.org/phobos/std_typecons.html#Rebindable

Guess I could do that. But would there be a difference if I just 
declared the restArgs as non const then? Given the objective is 
"set this var to point to this thing and not allow it to be set 
to point to anything else".


More information about the Digitalmars-d-learn mailing list