Re: RAII trouble
On Friday, 20 November 2015 at 23:35:50 UTC, Spacen Jasset wrote: On Friday, 20 November 2015 at 23:21:03 UTC, anonymous wrote: [...] FT_Init_FreeType must be read at compile time, because freeType is a module level variable, and initializers for module variables must be static values. The initializer is run through CTFE (Compile Time Function Evaluation). Put the initialization/assignment in a function and it should work. That function can be a static constructor or the main function: [...] Yes, I see. I made a mistake. I need to initialize it elsewhere. It was quite confusing. Thanks. I Just noticed this trick you posted, that's interesting. Of course I couldn't get it working without the void to discard the initialization; FreeType freeType = void; /* 'void' prevents default initialization */ static this() {freeType = FreeType.initialise();} /* should work */
Re: RAII trouble
On Friday, 20 November 2015 at 23:21:03 UTC, anonymous wrote: [...] FT_Init_FreeType must be read at compile time, because freeType is a module level variable, and initializers for module variables must be static values. The initializer is run through CTFE (Compile Time Function Evaluation). Put the initialization/assignment in a function and it should work. That function can be a static constructor or the main function: [...] Yes, I see. I made a mistake. I need to initialize it elsewhere. It was quite confusing. Thanks.
Re: RAII trouble
On 20.11.2015 23:56, Spacen Jasset wrote: The ideal would be to have a struct that can be placed inside a function scope, or perhaps as a module global variable. Why does Ft_Init_FreeType have to be read at compile time? text.d(143,15): Error: static variable FT_Init_FreeType cannot be read at compile time text.d(174,43):called from here: initialise() struct FreeType { @disable this(); static FreeType initialise() { return FreeType(0); } this(int) { int error = FT_Init_FreeType(&library); /// ERROR enforce(!error); } alias library this; ~this() { FT_Done_FreeType(library); } FT_Library library; } FreeType freeType = FreeType.initialise(); FT_Init_FreeType must be read at compile time, because freeType is a module level variable, and initializers for module variables must be static values. The initializer is run through CTFE (Compile Time Function Evaluation). Put the initialization/assignment in a function and it should work. That function can be a static constructor or the main function: FreeType freeType = void; /* 'void' prevents default initialization */ static this() {freeType = FreeType.initialise();} /* should work */ void main() {freeType = FreeType.initialise();} /* too */
Re: RAII trouble
On 11/20/2015 02:56 PM, Spacen Jasset wrote: > FT_Init_FreeType is a static which is set at some previous time to a > function entry point in the FreeType library. > text.d(143,15): Error: static variable FT_Init_FreeType cannot be read > at compile time The compiler seems to think that FT_Init_FreeType is a variable. Is that really a function pointer? Can you show how it's defined. My simple test works: extern (C) int init_func(double){ return 42; } static auto my_func = &init_func; struct S { static S init() { return S(0); } this(int) { my_func(1.5); } } void main() { auto s = S.init(); } Ali
RAII trouble
I have the following code in attempt to create an RAII object wrapper, but it seems that it might be impossible. The problem here is that FT_Init_FreeType is a static which is set at some previous time to a function entry point in the FreeType library. I could use a scope block, but would rather have a self contained thing to do the work. Perhaps some sort of mixin is the other solution? The ideal would be to have a struct that can be placed inside a function scope, or perhaps as a module global variable. Why does Ft_Init_FreeType have to be read at compile time? text.d(143,15): Error: static variable FT_Init_FreeType cannot be read at compile time text.d(174,43):called from here: initialise() struct FreeType { @disable this(); static FreeType initialise() { return FreeType(0); } this(int) { int error = FT_Init_FreeType(&library); /// ERROR enforce(!error); } alias library this; ~this() { FT_Done_FreeType(library); } FT_Library library; } FreeType freeType = FreeType.initialise();