Struct fields and properties as alias members

Ben Jones fake at fake.fake
Wed Dec 8 17:19:32 UTC 2021


I'm trying to use a property member of a struct as a template 
alias parameter and I don't really understand how to fix the 
error message I'm seeing (I also tried the simpler case of a 
plain struct member, and that didn't work either).  Is what I'm 
trying to do possible?  It seems like maybe I'd have to pass s as 
a runtime parameter to get it to work?

```
import std.stdio;

struct S{
     int a = 1;
     int b = 2;

     @property int c(){ return a; }
     @property int c(int newc) { a = newc; return a;}
}	

void func(alias field)(){
  	writeln(field);
         field = 5;
}
void main(){
     S s;
     func!(s.a)();
     func!(s.b)();

     func!(s.c)();
}
```
Runnable link here: https://run.dlang.io/is/WHM1Er

Errors:
```
onlineapp.d(17): Error: need `this` for `func` of type `@safe 
void()`
onlineapp.d(18): Error: need `this` for `func` of type `@safe 
void()`
onlineapp.d(12): Error: need `this` for `c` of type `@property 
int()`
onlineapp.d(13): Error: need `this` for `c` of type `@property 
int(int newc)`
onlineapp.d(20): Error: template instance `onlineapp.func!(c)` 
error instantiating
```


More information about the Digitalmars-d-learn mailing list