Making alias of a struct field needs "this".

realhet real_het at hotmail.com
Tue Jun 2 09:28:01 UTC 2020


On Tuesday, 2 June 2020 at 09:10:03 UTC, Ali Çehreli wrote:
> On 6/2/20 1:56 AM, realhet wrote:
>
> > struct A{
> >    struct B{ int c; }
> >    B b;
> >
> >    auto f(){
> >      alias d = b.c;
>
> The spec explicitly says it's not legal: "Aliases cannot be 
> used for expressions" (Item 10):
>
>   https://dlang.org/spec/declaration.html#alias
>
> I use nested functions for such cases:
>
>   auto f(){
>     auto d() {
>       return b.c;
>     }
>     return d;
>   }
>
> Ali

Thanks for responses:

I did it that way:
   private enum fieldMap = [ // simple names for descriptive and 
structured fields
     "hauteur"       : "general.height",
     "rayon"         : "profile.radius",
     "plage"         : "profile.plage",
     "offsetv"       : "profile.verticalOffset",
     "offseth"       : "profile.horizontalOffset",
     "varrad0"       : "profile.linearVariationBottom",
     "varrad1"       : "profile.linearVariationTop",
     "amplitude"     : "periphery.amplitude",
     "varlin0"       : "periphery.linearVariationBottom",
     "varlin1"       : "periphery.linearVariationTop",
     "varsinp"       : "periphery.cosinusVariationPlage",
     "varsino"       : "periphery.cosinusVariationOffset",
     "periodes"      : "periphery.nbPeriods",
     "revolution"    : "periphery.turn"
   ];

   static foreach(k, v; fieldMap){ mixin("@property auto $() 
const{ return #; }".replace("$", k).replace("#", v)); } //I know, 
i know -> AliasSeq :D

But it doesn't look nice.

I've found some additional info here: 
https://forum.dlang.org/thread/hresorfqicczofnrccpl@forum.dlang.org?page=2

Seems like I must not except the same from an alias template 
paramater and from the alias construct. The alias construct is 
for a single symbol.


More information about the Digitalmars-d-learn mailing list