www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixin template hide

reply Eppason <Epp son.com> writes:
I use self-introspection and have the case for having to "remove" 
things from the mixin.

Typically the mixin will mixin stuff that it does not exist in 
the scope it is trying to mix in, a great feature.

But what I would like to do is tell the mixin not to mix in 
foo(), this allows me to take a step back in the abstraction that 
I have created, rather than having to only move forward.

mixin FOO();

 disable FOO.foo(); // prevents foo from mixing in from FOO and 
does not add foo to the method table

with functions, I can create dummy functions, but they then still 
exist. For fields, I cannot remove.


mixin template FOO()
{
     void test()
     {
         // Self introspection
         static if (__traits(compiles, foo()))
              ...
     }
}

mixin template BAR()
{
     mixin FOO();
     void foo() { }
}

struct S
{
     mixin BAR();
     // We now have foo(); But I don't want!!!
}

You might gander that I could just mixin FOO and void this 
problem. Well, there are things in BAR I do want. I do know, 
because of design, that removing foo won't "break" bar, so there 
is no risk. It is more risk to create a dummy foo that doesn't do 
what FOO expects of it(which, is to foo).

For example, if foo is actually allocate_memory, then Foo works 
with allocate_memory or without(because of self-introspection). 
Bar adds the ability to allocate_memory, and print "Hello". For 
S, I want FOO & the ability to print "Hello" but not the ability 
to allocate.

The obvious solution is to refactor Bar, but in the real world, 
it is much harder and life would be much easier if I could remove 
foo from exists inside S. At worse, if Bar did depend on foo, I 
would simply get errors about missing foo.

I doubt it is possible, but maybe some tricks?
Jul 24 2016
parent arturg <var.spool.mail700 gmail.com> writes:
On Sunday, 24 July 2016 at 18:38:53 UTC, Eppason wrote:

 The obvious solution is to refactor Bar, but in the real world, 
 it is much harder and life would be much easier if I could 
 remove foo from exists inside S. At worse, if Bar did depend on 
 foo, I would simply get errors about missing foo.

 I doubt it is possible, but maybe some tricks?
you could define a flag for foo, like: mixin template BAR(bool addFoo = true) { mixin FOO; static if(addFoo) { void foo(){} } }
Jul 24 2016