How to declare immutable struct outside of try catch and reference it later
Basile B.
b2.temp at gmx.com
Sun Dec 3 23:29:01 UTC 2017
On Sunday, 3 December 2017 at 22:33:40 UTC, kdevel wrote:
> On Sunday, 3 December 2017 at 14:58:03 UTC, Basile B. wrote:
>> 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
>> }
>> ---
>
> When config is null, e.g. in case "load some file..." threw,
> you get a segfault. No error handling at all!
I don't follow you...the file thing happens in the __ctor.
Exceptions are well handled. Maybe you've missed the try (w/o
braces) ?
---
immutable struct Configuration {
this(string){/*parse_config...*/}
}
Configuration* config;
int main() {
try {
config = new Configuration("config.sdl");
// instead of "conf = parse_config("config.sdl");"
} catch(Exception e){
std.stdio.stderr.writeln("Error reading configuration
file: ", e.msg);
return 1;
}
}
---
More information about the Digitalmars-d-learn
mailing list