How to declare immutable struct outside of try catch and reference it later
kdevel
kdevel at vogtner.de
Mon Dec 4 19:13:56 UTC 2017
On Sunday, 3 December 2017 at 23:29:01 UTC, Basile B. wrote:
> 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.
But not in the code you provided first. You used to ignore the
exception:
catch(Exception){}
> Maybe you've missed the try (w/o braces) ?
Not really.
> ---
> 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;
> }
> }
> ---
Are my eyes too weak or is my compiler too old? For this piece of
code
test.d
```
immutable struct C {
this (string filename)
{
}
}
void main ()
{
auto config = new C ("config.sdl");
}
```
it says: test.d(9): Error: immutable method test.C.this is not
callable using a mutable object
→ https://issues.dlang.org/show_bug.cgi?id=13628
→
http://forum.dlang.org/thread/mailman.926.1413747186.9932.digitalmars-d-bugs@puremagic.com
More information about the Digitalmars-d-learn
mailing list