On 5/22/18 4:40 AM, Robert M. Münch wrote:
On 2018-05-21 18:55:36 +0000, Steven Schveighoffer said:

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.

The type is the problem. The type returned by filter is parameterized on that *specific* lambda. If you look at the error message, it says something like "lamda1" and "lambda4" in the type for filter.

In order to make this work, the compiler would have to make the name of the lambda based on the actual AST inside it. I think something like that should be done.


I see you are casting now as well,

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

Haha, looking back, I see you didn't cast originally, which is probably the reason it didn't work :)

Here is the line from your revised example after Jonathan showed you how to declare a member of that type:

    myB.mySubstream = myA.myStream.filter!(x => x == myMessage);

And here is the subsequent line:

myB.mySubstream = cast(myMessageType)myA.myStream.filter!(x => x == myMessage);

Both exactly the same, but one forces the cast. Your first line could have been done:

myB.mySubstream = cast(typeof(myB.mySubstream))myA.myStream.filter!(x => x == myMessage);

Giving a name helps to make the code less verbose, but essentially that is what you are doing -- forcing the cast.

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

Well, you then have to have 50-100 types of b with the correct member. Unless... you want to parameterize b, in which case it becomes REALLY easy:

class b(FilterType)
{
   FilterType mySubstream;
}

auto makeB(FilterType)(FilterType f)
{
   return new b!FilterType(f);
}

...

auto myB = myA.myStream.filter!(x => coolCrazyFunction(x)).makeB;

-Steve

Reply via email to