How to create a template class using foreach delegate to filter objects in a member function call?

Robert M. Münch robert.muench at saphirion.com
Fri May 31 16:24:28 UTC 2019


On 2019-05-31 11:07:00 +0000, Alex said:

> Not sure, if I understood your problem correctly.

I can imagine... I try my best :-)

> It is meant that the class myClass defines an array of myOtherClass objects?

Yes. So there is one class having an array of other stuff.

> The code does not compile and it does not provide an example, how you 
> would apply the pattern, even in a non-compileable way...

The code is just to show the problem and not meant to compile. I 
couldn't get anything to compile...

> However, commonly, a filter is a higher order function, which expects a 
> predicate acting on each element of a set. Even if it's higher order, 
> it is still a function, not a delegate. Therefore, it is unexpected, 
> that you want to store something inside the filter.

I choose filter to give a hint what the idea is, not meant to be that I 
want to use a filter.

> 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 summurize 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.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list