Const Variables

Salih Dincer salihdb at hotmail.com
Sun Apr 3 18:24:09 UTC 2022


On Sunday, 3 April 2022 at 14:29:22 UTC, Paul Backus wrote:
> On Sunday, 3 April 2022 at 13:50:28 UTC, Salih Dincer wrote:
>> Hi all,
>>
>> Do you have a good example of how const variables actually 
>> work?
>>
>> So I'm not talking about system resources and programming 
>> errors.  I want to say it's good.  Because if everything works 
>> without it, why does it exist?
>
> This is covered in the const FAQ:
>
> https://dlang.org/articles/const-faq.html

The programs I write are not multithreaded.  Also, I don't work 
as a team.  Thanks for the link but there is only the following 
example:

```d
struct Foo
{
     mutable int len;
     mutable bool len_done;
     const char* str;
     int length()
     {
         if (!len_done)
         {
             len = strlen(str);
             len_done = true;
         }
         return len;
     }
     this(char* str) { this.str = str; }
}
const Foo f = Foo("hello");
bar(f.length);
```

Moreover, I did not understand anything from the example in the 
article :)

SDB at 79


More information about the Digitalmars-d-learn mailing list