`static` function ... cannot access variable in frame of ...

Bastiaan Veelo Bastiaan at Veelo.net
Mon Jan 15 18:16:44 UTC 2024


Hey people, I can use some help understanding why the last line 
produces a compile error.

```d
import std.stdio;

struct S
{
     static void foo(alias len)()
     {
         writeln(len);
     }
}

void S_foo(alias len)()
{
     writeln(len);
}

void main()
{

     const five = 5;
     S_foo!five;	// Fine
     S.foo!five; // Error
}
```

The error is
```
onlineapp.d(7): Error: `static` function 
`onlineapp.main.foo!(5).foo` cannot access variable `five` in 
frame of function `D main`
onlineapp.d(19):        `five` declared here
onlineapp.d(21): Error: template instance 
`onlineapp.main.foo!(5)` error instantiating
```

It seems to me this should just work.

Thanks!

--Bastiaan.


More information about the Digitalmars-d-learn mailing list