www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - File[string] associative array & initialization of static member

reply Vidar Wahlberg <canidae exent.net> writes:
Take a look at the following input:

naushika:~/tmp> cat foo.d
import std.stdio;
File[string] files;
void main() {
	files["test"] = File("test", "w");
	files["test"].writefln("meh: %s", "heh");
}
naushika:~/tmp> dmd foo.d
Internal error: ../ztc/cgcs.c 162
naushika:~/tmp> dmd foo.d -O
naushika:~/tmp> ./foo
object.Exception std/stdio.d(1131): Enforcement failed
----------------
./foo(pure  safe bool std.exception.enforce!(bool, "std/stdio.d", 
1131).enforce(bool, lazy const(char)[])+0x45) [0x45711d]
./foo(std.stdio.File.LockingTextWriter 
std.stdio.File.LockingTextWriter.__ctor(ref std.stdio.File)+0x38) [0x4564ac]
./foo( property std.stdio.File.LockingTextWriter 
std.stdio.File.lockingTextWriter()+0x29) [0x4565d9]
./foo(void std.stdio.File.writefln!(immutable(char)[], 
immutable(char)[]).writefln(immutable(char)[], immutable(char)[])+0x44) 
[0x44816c]
./foo(_Dmain+0x10f) [0x44811b]
./foo(extern (C) int rt.dmain2.main(int, char**).void runMain()+0x17) 
[0x452e1f]
./foo(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void 
delegate())+0x2a) [0x4529c6]
./foo(extern (C) int rt.dmain2.main(int, char**).void runAll()+0x42) 
[0x452e72]
./foo(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void 
delegate())+0x2a) [0x4529c6]
./foo(main+0xd3) [0x452957]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xfd) [0x7fbe6cb5cead]
----------------


My first question would be: Why do I get an internal error when I try to 
compile this code without optimization flag? I'm using "DMD64 D Compiler 
v2.057".

Then the second question would be:
I'm writing a simple module for logging, it's going to write to some few 
files over a longer duration and my idea was to keep a reference to the 
handler instead of calling the constructor of File all the time.
Apparently the way I've tried above was not the right way to do it, any 
suggestions would be most welcome.


And finally a question about initialization of static members:
Why can't you write "static Foo foo = new Foo();"?
Instead you'll have to create a static constructor that pretty much does 
the same thing, just using more lines:
static Foo foo;
static this() {
   foo = new Foo();
}

Is it not possible for the compiler to do this for me?
Feb 12 2012
parent Artur Skawina <art.08.09 gmail.com> writes:
On 02/12/12 21:21, Vidar Wahlberg wrote:
 Take a look at the following input:
 
 naushika:~/tmp> cat foo.d
 import std.stdio;
 File[string] files;
 void main() {
     files["test"] = File("test", "w");
     files["test"].writefln("meh: %s", "heh");
 }
 naushika:~/tmp> dmd foo.d
 Internal error: ../ztc/cgcs.c 162
 naushika:~/tmp> dmd foo.d -O
 naushika:~/tmp> ./foo
 object.Exception std/stdio.d(1131): Enforcement failed
 ----------------
 ./foo(pure  safe bool std.exception.enforce!(bool, "std/stdio.d",
1131).enforce(bool, lazy const(char)[])+0x45) [0x45711d]
 ./foo(std.stdio.File.LockingTextWriter
std.stdio.File.LockingTextWriter.__ctor(ref std.stdio.File)+0x38) [0x4564ac]
 ./foo( property std.stdio.File.LockingTextWriter
std.stdio.File.lockingTextWriter()+0x29) [0x4565d9]
 ./foo(void std.stdio.File.writefln!(immutable(char)[],
immutable(char)[]).writefln(immutable(char)[], immutable(char)[])+0x44)
[0x44816c]
 ./foo(_Dmain+0x10f) [0x44811b]
 ./foo(extern (C) int rt.dmain2.main(int, char**).void runMain()+0x17)
[0x452e1f]
 ./foo(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void
delegate())+0x2a) [0x4529c6]
 ./foo(extern (C) int rt.dmain2.main(int, char**).void runAll()+0x42) [0x452e72]
 ./foo(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void
delegate())+0x2a) [0x4529c6]
 ./foo(main+0xd3) [0x452957]
 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xfd) [0x7fbe6cb5cead]
 ----------------
 
 
 My first question would be: Why do I get an internal error when I try to
compile this code without optimization flag? I'm using "DMD64 D Compiler
v2.057".
 
 Then the second question would be:
 I'm writing a simple module for logging, it's going to write to some few files
over a longer duration and my idea was to keep a reference to the handler
instead of calling the constructor of File all the time.
 Apparently the way I've tried above was not the right way to do it, any
suggestions would be most welcome.
Hmm, the 'files["test"] = File' assignment causes the program to segfault when compiled with GDC. std.stdio.File.opAssign() calls the dtor which crashes. But why is opAssign being used here? artur
Feb 12 2012