www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can you get typeof(this) in a mixin template - trying to mimic a

reply aliak <something something.com> writes:
Hi,

I'm trying to do something similar to what Kotlin allows with 
assigning to member variables from a map. The syntax is very 
readable and looks like:

class User(val map: Map<String, Any?>) {
     val name: String by map
     val age: Int     by map
}

So I'm trying to do something similar in D:

mixin template MapMembers(alias aa) {
   foreach (name; typeof(this).tupleof) {
     // if name is in aa, then mixin(m = aa[" ~ m ~ "]) ... ish
   }
}
struct User {
   this(Variant[string] aa) {
     mixin MapMembers!aa;
   }
}

Seems I can't do typeof(this) inside the mixin.

One workaround is to explicitly pass the type "User" to the 
template mixin, but it feels like this is something that should 
work and I'm just unfamiliar with template mixins and their usage.

Cheers,
- Ali

[0] 
https://kotlinlang.org/docs/reference/delegated-properties.html#storing-properties-in-a-map
Aug 28 2018
parent reply Alex <sascha.orlov gmail.com> writes:
On Tuesday, 28 August 2018 at 20:39:16 UTC, aliak wrote:
 Hi,

 I'm trying to do something similar to what Kotlin allows with 
 assigning to member variables from a map. The syntax is very 
 readable and looks like:

 class User(val map: Map<String, Any?>) {
     val name: String by map
     val age: Int     by map
 }

 So I'm trying to do something similar in D:

 mixin template MapMembers(alias aa) {
   foreach (name; typeof(this).tupleof) {
     // if name is in aa, then mixin(m = aa[" ~ m ~ "]) ... ish
   }
 }
 struct User {
   this(Variant[string] aa) {
     mixin MapMembers!aa;
   }
 }

 Seems I can't do typeof(this) inside the mixin.

 One workaround is to explicitly pass the type "User" to the 
 template mixin, but it feels like this is something that should 
 work and I'm just unfamiliar with template mixins and their 
 usage.

 Cheers,
 - Ali

 [0] 
 https://kotlinlang.org/docs/reference/delegated-properties.html#storing-properties-in-a-map
Isn't the problem, that inside a template a declaration is expected, and not a foreach? This works as expected: ´´´ import std.experimental.all; void main() { writeln("Edit source/app.d to start your project."); } mixin template MapMembers(alias aa) { typeof(this) var; /* foreach (name; typeof(this).tupleof) { // if name is in aa, then mixin(m = aa[" ~ m ~ "]) ... ish } */ } struct User { this(Variant[string] aa) { mixin MapMembers!aa; } } //mixin MapMembers!(Variant[string].init); ´´´
Aug 28 2018
parent aliak <something something.com> writes:
On Tuesday, 28 August 2018 at 20:58:23 UTC, Alex wrote:
 Isn't the problem, that inside a template a declaration is 
 expected, and not a foreach?
Boh, you're right. I guess I misunderstood mixin templates. Found a way with a normap function though :p void mapSelf(alias self, alias aa)() { ... } this(map) { mapSelf!(this, map); } https://run.dlang.io/is/EhDpOM
Aug 28 2018