How to declare immutable struct outside of try catch and reference it later

Basile B. b2.temp at gmx.com
Sun Dec 3 14:58:03 UTC 2017


On Sunday, 3 December 2017 at 05:49:54 UTC, Fra Mecca wrote:
> I have this code:
>     Configuration conf = void ;
>     try {
>         conf = parse_config("config.sdl");
>     } catch (Exception e) {
>         std.stdio.stderr.writeln("Error reading configuration 
> file: ", e.msg);
>         exit(1);
>     }
>
> // other code
> function(value, conf);
> // end
>
> I get:
> source/app.d(18,3): Error: cannot modify struct conf 
> Configuration with immutable members
>
> Is there a way to declare conf outside of the try catch block 
> and use it later?
> I thought void explicitly avoid inizialization.

In this case i'd go for a typed pointer, e.g

---
immutable struct Configuration
{
     this(string){/*load some file...*/}
     int value;
}

Configuration* config;

void main()
{
     try config = new Configuration("config.sdl");
     catch(Exception){}
     // config.value = 42; // ok, read only
}
---


More information about the Digitalmars-d-learn mailing list