On Friday, 31 May 2019 at 16:24:28 UTC, Robert M. Münch wrote:
The code is just to show the problem and not meant to compile. I couldn't get anything to compile...

That's ok, but could you provide an example anyway? Is it like this?

´´´
void main(){
    auto target = new myClass!int();
    target.objects.length = 4;
    auto val = 42;
put(target, val, testfunction); // does the test function enters here?
    put(target, val);
auto f = myFilter!int; // where do you want to use this entity?
}
´´´

Said this, I for myself had a similar problem. I solved this by reversing the hierarchy: I templated my objects I wanted to use the filter on with the filter function and removed the need of the template parameter inside the filter.

The thing is, myClass is not under my control. It's coming from a library I don't maintain and I don't want to mess around with the code or if, as minimalistic as possible. That's why I was thinking about providing a put(T)... function.

*


My first idea was to sub-class myClass, but objects is private, so no chance to get access to it.

You could still write a general filter function in this case, if you want. For example, you could use mixins for this...

Then myClass needs to somehow get the mixin in.

So, to summarize the problem: Given a class that manages an array of things as an OutputRange how can I provide a put() function with something like a filter-predicate? I only want to put() to some of the things, not all.

Do you have control about the contained classes? If so, it is a hint to implement the testing inside them. Like:

/* probably inside a templated mixin */
bool testfunction(inputs){...}

class myOtherClass(/*probably templated*/){... mixin testfunction ... & provide a put function}

* So, you are not totally against the idea of modifying the foreign library, but you want to keep modifications small? With the approach now, you could, for example, handle compile time blocks inside the put function in the myClass and dynamical ones inside the myOtherClasses.

class myClass(E){/* inserted put function */ void put(...){static if put action is at all possible --> put. }}

Reply via email to