auto & class members

Robert M. Münch robert.muench at saphirion.com
Tue May 22 08:40:55 UTC 2018


On 2018-05-21 18:55:36 +0000, Steven Schveighoffer said:

> So the issue here is that the lambda function inside myFunc is 
> DIFFERENT than the one inside b. They are both the same function, but 
> with essentially different names.

Aha... that explains it pretty good.

> When you use the alias, both are using the same exact lambda.

Ok. I didn't expect that the name is relevant in this case, instead 
assumed that only the types need to match.

> I see you are casting now as well,

Do I? Not that I'm aware of it in my pseudo-code example...


> What may make more sense (both for type sanity and for code reuse) is 
> to wrap your call to filter into one place so it can be used wherever 
> you need it:
> 
> auto wrapStream(S)(S str) { return str.filter!(x => x == myMessage); }
> 
> class b
> {
>     typeof(wrapStream(a.init.myStream)()) mySubStream;
> }
> 
> void myFunc() {
>     a myA = new a;
>     b myB = new b;
>     myB.mySubstream = myA.myStream.wrapStream;
> }

This would require one wrap function per different lambda, right? 
Assume I have 50-100 of these. Maybe the myMessage value can be given 
as parameter and with this becomes more like a "filter factory". Not 
sure if this would work.

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



More information about the Digitalmars-d-learn mailing list