www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading an environment variable value

reply Josh Holtrop <jholtrop gmail.com> writes:
I am trying to use `std.process.environment.get()` as described 
here: https://dlang.org/library/std/process/environment.get.html

I wrote this program to test it:

```d
import std.stdio;
import std.process;

int main(string[] args)
{
     string home = environment.get("HOME");
     writeln("home is ", home);
     return 0;
}
```

But it fails to compile:

```
$ ldc2 -of environment environment.d
environment.d(6): Error: template `object.get` cannot deduce 
function from argument types `!()(string)`
/usr/lib/ldc/x86_64-linux-gnu/include/d/object.d(3230):        
Candidates are: `get(K, V)(inout(V[K]) aa, K key, lazy inout(V) 
defaultValue)`
/usr/lib/ldc/x86_64-linux-gnu/include/d/object.d(3237):           
              `get(K, V)(inout(V[K])* aa, K key, lazy inout(V) 
defaultValue)`
```

It works, though, to switch to a static import of `std.process`:

```d
import std.stdio;
static import std.process;

int main(string[] args)
{
     string home = std.process.environment.get("HOME");
     writeln("home is ", home);
     return 0;
}
```

Why? What am I doing wrong in the first version that does not use 
the `static import`?

My ldc2 version:

```
$ ldc2 -version
LDC - the LLVM D compiler (1.28.0):
   based on DMD v2.098.0 and LLVM 11.1.0
   built with LDC - the LLVM D compiler (1.28.0)
   Default target: x86_64-pc-linux-gnu
   Host CPU: skylake
   http://dlang.org - http://wiki.dlang.org/LDC
```
Jun 29 2023
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Thursday, 29 June 2023 at 18:47:48 UTC, Josh Holtrop wrote:
 $ ldc2 -of environment environment.d
Since you named the file `environment.d` and didn't use an explicit `module name.thing;` declaration, the compiler assumes it should match the filename. So it injects an implicit `module environment;` to the top of the file. Now when you mention `environment`, it uses *that* name instead of the imported name, so it thinks you are trying to pass it the module called environment instead of the object. If you add `module yourapp.environment;` or something to the top it'd fix it (unless you tried to use something called `yourapp`, then the name might conflict again!) or rename the file.
Jun 29 2023
parent Josh Holtrop <jholtrop gmail.com> writes:
On Thursday, 29 June 2023 at 19:19:21 UTC, Adam D Ruppe wrote:
 On Thursday, 29 June 2023 at 18:47:48 UTC, Josh Holtrop wrote:
 $ ldc2 -of environment environment.d
Since you named the file `environment.d` and didn't use an explicit `module name.thing;` declaration, the compiler assumes it should match the filename. So it injects an implicit `module environment;` to the top of the file. Now when you mention `environment`, it uses *that* name instead of the imported name, so it thinks you are trying to pass it the module called environment instead of the object. If you add `module yourapp.environment;` or something to the top it'd fix it (unless you tried to use something called `yourapp`, then the name might conflict again!) or rename the file.
Ah, that makes sense, and yes that fixed it. Thank you!
Jun 29 2023