Error: getenv cannot be interpreted at compile time

Paul via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 17 11:56:19 PST 2015


On Tuesday, 17 February 2015 at 19:29:52 UTC, Adam D. Ruppe wrote:
> On Tuesday, 17 February 2015 at 19:17:42 UTC, Paul wrote:
>> I don't understand the error and Google doesn't help - can it 
>> be fixed or am I just using the wrong approach?
>
> Trying to create it as a global tries to make it as a static 
> variable which means it constructs at compile time. It can't 
> access the TERM environment variable at compile time and that 
> causes the error.
>
> Putting Terminal at global scope isn't really ideal, there'd be 
> potential destructor problems (the terminal needs to be cleaned 
> up at program termination). You could stick a pointer to it up 
> there though:
>
>
> Terminal* terminal; // maybe make it shared too but i'd try to 
> avoid that, terminal isn't quite thread safe
>
>
> void main() {
>    auto main_terminal = Terminal(options...);
>    terminal = &main_terminal;
>    scope(exit) terminal = null; // clean up the pointer too 
> when it goes invalid
>    // the rest of your program goes normally
>    // and other functions can use the global pointer
> }
>
>
>
> Just make sure you set it in main before doing anything else so 
> the pointer isn't null and you should be good to go. Then when 
> main returns, it will clean up.

I see, thanks once again :D I don't know why the environment 
variable isn't accessible at compilation (from a terminal) but 
that's OS business I guess rather than D related.

I did try something similar to your solution but not using a 
pointer - I'd prefer to avoid using pointers if at all possible!


More information about the Digitalmars-d-learn mailing list