Pointer to environment.get

Basile B. b2.temp at gmx.com
Mon Aug 28 10:20:14 UTC 2023


On Monday, 28 August 2023 at 06:38:50 UTC, Vino wrote:
> Hi All,
>
>   The the below code is not working, hence requesting your help.
>
> Code:
> ```
> import std.stdio;
> import std.process: environment;
> void main () {
>    int* ext(string) = &environment.get("PATHEXT");
>    writeln(*ext);
> }
> ```

Problems is that "PATHEXT" is a runtime argument. If you really 
want to get a pointer to the function for that runtime argument 
you can use a lambda:

```d
import std.stdio;
import std.process: environment;
void main () {

     alias atGet = {return environment.get("PATHEXT");}; // really 
lazy

     writeln(atGet);         // pointer to the lambda
     writeln((*atGet)());    // call the lambda
}
```

There might be other ways, but less idiomatic (using a struct + 
opCall, a.k.a  a "functor")


More information about the Digitalmars-d-learn mailing list