static function and access frame

Alex sascha.orlov at gmail.com
Tue Jan 23 21:51:16 UTC 2018


Ok, I'm quite sure, I overlooked something.

First version, working

[code]
void main()
{
	auto s = S();
	auto t = T!s();
	t.fun;
}
struct S { void fun(){} }
struct T(alias s){ auto fun() { s.fun; } }
[/code]

Now, the fun method of struct T has to become static and the 
problems begin:
Error: static function app.main.T!(s).T.fun cannot access frame 
of function D main

Ok, found somewhere, that it could help to move the static 
function outside of the struct and tried this.

Second version, not working

[code]
void main()
{
	auto s = S();
	auto t = T!s();
	fun!(typeof(t));
}
struct S { void fun(){} }
struct T(alias s){  }
auto fun(T : T!s, alias s)() { s.fun; }
[/code]

 From my point of view, the second version is the most promising 
one, if there are problems with the first one. However, I didn't 
figured it out, how to match the alias template parameter to be 
able to call it from within the function fun (which is now at 
module level) directly.

Ok. Now, trying to find a solution I wrote the third version, 
working

[code]
void main()
{
	auto s = S();
	auto t = T!s();
	fun(t);
}
struct S { void fun(){} }
struct T(alias s){ auto ss(){return s; } }
auto fun(T)(T t) { t.ss.fun; }
[/code]

Is this meant to be the right way? I mean, ok, if so, then, the 
way from different frames is a little bit verbose, but working. 
However, the fun method is meant to not use a specific object 
from the time point, where it was marked static. Why should I 
pass an instance to it?


More information about the Digitalmars-d-learn mailing list