Create template expression sequence
Paul Backus
snarwin at gmail.com
Thu Dec 2 16:58:40 UTC 2021
On Thursday, 2 December 2021 at 15:54:17 UTC, Menshikov wrote:
> ```d
> import std.meta, std.stdio;
>
> auto get0(A...)() => A[0];
>
> struct A{int a = 4; int b = 2;}
>
> void main(){
> AliasSeq!(4, 2)[0].writeln;
> get0!(4, 2).writeln;
> A a;
> a.tupleof[0].writeln;
> get0!(a.tupleof).writeln;//error :(
> }
> ```
Easiest fix is to change `get0` to take its arguments as runtime
parameters rather than template parameters:
```d
auto get0(Args...)(auto ref Args args) => args[0];
```
Full example: https://run.dlang.io/is/5LVeyY
> ```d
> enum Attr;
>
> struct A{
> int sample(float a) => cast(int) a + 1;//nope
> @Attr int sample(int a) => a;//yep
> }
>
>
> void func(A)(in A a, int arg){
> static foreach(symbol; getSymbolsByUDA!(A, Attr)){
> /* and so, I need to call the `symbol` method via a.
> This is where the problems begin,
> because if I just can't do this mixin ("a." ~
> Field.stringof)
> I lose which overload I need to use */
> }
> }
> ```
You can use [`__traits(child)`][1] to bind a member symbol to an
object:
```d
void func(A)(A a, int arg){
import std.traits, std.stdio;
static foreach(symbol; getSymbolsByUDA!(A, Attr)){
writeln(__traits(child, a, symbol)(arg));
}
}
```
Full example: https://run.dlang.io/is/Dg6INi
[1]: https://dlang.org/spec/traits.html#child
> ```d
> enum Attr;
>
> class B{
> @Attr int a;
> }
>
> class A: B{
> @Attr int b;
> }
>
> void main(){
> pragma(msg, getSymbolsByUDA!(A, Attr));//ERROR!
> }
> ```
Bug report: https://issues.dlang.org/show_bug.cgi?id=17870
Looks like there was an attempt to fix it that got abandoned
close to the finish line: https://github.com/dlang/dmd/pull/11633
More information about the Digitalmars-d
mailing list