www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why are template alias parameters broken?

reply Menshikov <mensikovk817 gmail.com> writes:
It's work:

```d
template Foo(alias var)
{
     void inc() { var++; }
}

void main()
{
     int v = 4;
     alias foo = Foo!(v);
     foo.inc();
     assert(v == 5);
}
```
But it doesn't work:
```d
template Foo(alias var)
{
     void inc() { var++; }
}

void main()
{
     struct V{
       int a;
     }
     auto v = V(4);
     alias foo = Foo!(v.a);
     foo.inc();//err
     assert(v.a == 5);
}
```
Hence this applies to Alias!() and AliasSeq!()
Aug 27
next sibling parent reply Menshikov <mensikovk817 gmail.com> writes:
On Friday, 27 August 2021 at 18:47:56 UTC, Menshikov wrote:
 It's work:

 ```d
 template Foo(alias var)
 {
     void inc() { var++; }
 }

 void main()
 {
     int v = 4;
     alias foo = Foo!(v);
     foo.inc();
     assert(v == 5);
 }
 ```
 But it doesn't work:
 ```d
 template Foo(alias var)
 {
     void inc() { var++; }
 }

 void main()
 {
     struct V{
       int a;
     }
     auto v = V(4);
     alias foo = Foo!(v.a);
     foo.inc();//err
     assert(v.a == 5);
 }
 ```
 Hence this applies to Alias!() and AliasSeq!()
And it doesn't work ```d template Foo(alias var) { void inc() { var++; }//err } void main() { static struct V{ int a; } auto v = V(4); mixin Foo!(v.a)foo;//err foo.inc(); assert(v.a == 5); } ```
Aug 27
parent Menshikov <mensikovk817 gmail.com> writes:
On Friday, 27 August 2021 at 19:00:27 UTC, Menshikov wrote:
 On Friday, 27 August 2021 at 18:47:56 UTC, Menshikov wrote:
 It's work:

 ```d
 template Foo(alias var)
 {
     void inc() { var++; }
 }

 void main()
 {
     int v = 4;
     alias foo = Foo!(v);
     foo.inc();
     assert(v == 5);
 }
 ```
 But it doesn't work:
 ```d
 template Foo(alias var)
 {
     void inc() { var++; }
 }

 void main()
 {
     struct V{
       int a;
     }
     auto v = V(4);
     alias foo = Foo!(v.a);
     foo.inc();//err
     assert(v.a == 5);
 }
 ```
 Hence this applies to Alias!() and AliasSeq!()
And it doesn't work ```d template Foo(alias var) { void inc() { var++; }//err } void main() { static struct V{ int a; } auto v = V(4); mixin Foo!(v.a)foo;//err foo.inc(); assert(v.a == 5); } ```
We open it ourselves, and it works: ```d void main() { static struct V{ int a; } auto v = V(4); void inc() { v.a++; } inc(); assert(v.a == 5); } ```
Aug 27
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/27/21 2:47 PM, Menshikov wrote:
 It's work:
 
 ```d
 template Foo(alias var)
 {
      void inc() { var++; }
 }
 
 void main()
 {
      int v = 4;
      alias foo = Foo!(v);
      foo.inc();
      assert(v == 5);
 }
 ```
 But it doesn't work:
 ```d
 template Foo(alias var)
 {
      void inc() { var++; }
 }
 
 void main()
 {
      struct V{
        int a;
      }
      auto v = V(4);
      alias foo = Foo!(v.a);
      foo.inc();//err
      assert(v.a == 5);
 }
 ```
 Hence this applies to Alias!() and AliasSeq!()
Aliases to expressions don't work. They have to be symbols, type keywords (e.g. `int`), or compile-time values. `v.a` is an expression. It's quite an annoying limitation, I agree. To work around, you can do: ```d template Foo(alias var) { void inc() {var.a++} } ``` and then pass `v` instead of `v.a`. -Steve
Aug 27
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/27/21 3:14 PM, Steven Schveighoffer wrote:

 To work around, you can do:
 
 ```d
 template Foo(alias var)
 {
     void inc() {var.a++}
 }
 ```
 
 and then pass `v` instead of `v.a`.
Another possible workaround: ```d auto v = V(4); ref int a() { return v.a; } alias foo = Foo!a; foo.inc(); ``` -Steve
Aug 27
parent reply Menshikov <mensikovk817 gmail.com> writes:
On Friday, 27 August 2021 at 19:32:49 UTC, Steven Schveighoffer 
wrote:
 On 8/27/21 3:14 PM, Steven Schveighoffer wrote:

 To work around, you can do:
 
 ```d
 template Foo(alias var)
 {
     void inc() {var.a++}
 }
 ```
 
 and then pass `v` instead of `v.a`.
Another possible workaround: ```d auto v = V(4); ref int a() { return v.a; } alias foo = Foo!a; foo.inc(); ``` -Steve
Thanks
Aug 27
parent Menshikov <mensikovk817 gmail.com> writes:
On Friday, 27 August 2021 at 20:07:06 UTC, Menshikov wrote:

 Thanks
it would be cool if template alias parameters could work with expressions. And whether it would break the previous code.
Aug 27