www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Remove stuff from a template mixin

reply Gorge Jingale <Frifj mail.com> writes:
Is there a way to remove functions and fields that a mixin 
template adds?

I use mixin templates to create other types contents, like

struct A { mixin Stuff(); }

But Sometimes I only want some of the stuff.

struct B { mixin Stuff();  disable Add(); }

B is like an A but doesn't have the ability to add.

While I could do something like move the Add function to another 
template, it gets messy quickly because all I want to do is 
remove a feature, not create a hierarchical system like oop.

I tried making add private, but b.Add() still calls Stuff.Add.
Jul 26 2016
parent reply Gorge Jingale <Frifj mail.com> writes:
I might want to actually use Add internally in B so I can add 
some elements behind the scenes, I do not want to expose it to 
the outside world though.
Jul 26 2016
parent reply Jerry <Kickupx gmail.com> writes:
On Tuesday, 26 July 2016 at 19:02:32 UTC, Gorge Jingale wrote:
 I might want to actually use Add internally in B so I can add 
 some elements behind the scenes, I do not want to expose it to 
 the outside world though.
There are no way to remove things from an template directly. But you could however generate a new type which does not carry the specified members. This requires two steps: *The member filtering part *Generation part For the masking part take a look at https://forum.dlang.org/post/nn8gj8$6s5$1 digitalmars.com What you basicly do is that you iterate the members and based on some condition filters out the members you don't want. Then for generating you have to handle functions and fields. For fields a string concatenated with other strings fields is probably good enough. Something like this: "typeof(" ~ fullyQualifiedName!Aggregate ~ "." ~ memberName ~ ") " ~ member;"
Jul 26 2016
parent Gorge Jingale <Frifj mail.com> writes:
On Tuesday, 26 July 2016 at 22:23:37 UTC, Jerry wrote:
 On Tuesday, 26 July 2016 at 19:02:32 UTC, Gorge Jingale wrote:
 I might want to actually use Add internally in B so I can add 
 some elements behind the scenes, I do not want to expose it to 
 the outside world though.
There are no way to remove things from an template directly. But you could however generate a new type which does not carry the specified members. This requires two steps: *The member filtering part *Generation part For the masking part take a look at https://forum.dlang.org/post/nn8gj8$6s5$1 digitalmars.com What you basicly do is that you iterate the members and based on some condition filters out the members you don't want. Then for generating you have to handle functions and fields. For fields a string concatenated with other strings fields is probably good enough. Something like this: "typeof(" ~ fullyQualifiedName!Aggregate ~ "." ~ memberName ~ ") " ~ member;"
That seems like a lot of work just to remove some elements. Creating a whole new template. I think using additive composition would be easier and more directly(a few lines of code to split the template), and I guess is the more natural way.
Jul 26 2016