auto & class members
Robert M. Münch
robert.muench at saphirion.com
Sun May 20 17:40:39 UTC 2018
On 2018-05-20 14:49:59 +0000, Jonathan M Davis said:
> In cases like this, typeof is your friend. e.g. something like
>
> typeof(myStream.filter!(a => a == myMessage)) mySubStream;
Hi Jonathan, great! This got me a step further. So I can declare my
member now. But I get an implict cast error when I try:
class a {
... myStream;
}
class b {
typeof(a.myStream.filter!(x => x == myMessage)) mySubStream;
}
void myFunc() {
a myA = new a();
b myB = new b();
myB.mySubstream = myA.myStream.filter!(x => x == myMessage);
}
This gives (unnecessary stuff stripped):
Error: cannot implicitly convert expression filter(...) of type
app.myFunc.filter!(x => x == myMessage) to app.b.filter!(x => x ==
myMessage)
Why is myFunc now entering the game? I mean it's just the function
containing the code. It seems that:
typeof(myA.myStream.filter!(x => x == myMessage))
and
typeof(a.myStream.filter!(x => x == myMessage)) are not the same.
But inside class b I can't use a specific variable instance. And in
myFunc, I can't use a class type but need the specific instance.
Any further idea?
> though you might have trouble with the lambda being subtly different type
> even if you replace myMessage in it with something that will work in the
> scope that mySubStream is being declared.
Not sure if the above problem is exactly what you mention here. This is
all pretty tricky.
> However, that could be fixed by
> doing something like replacing the lambda with a free function or just
> creating a function that returns what you want to assign to mySubStream.
> Then you could just do something like
>
> typeof(myHelperFunc(myStream)) mySubStream;
>
> The exact solution can get a bit annoying in cases like this (it's arguably
> the biggest downside to auto returns), but typeof does provide a way out if
> you can get the expression to it to work. It is easier with local variables
> than member variables though, since you don't necessarily have everything
> you want to use in the expression that will give the variable its value
> available at the point that the variable is declared - but that's why
> a helper function can help, since it provides a way to encapsulate the
> expression and reuse it between the assignment and the declaration.
Not sure I understand every aspect but it's getting clearer... Thanks so far.
--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
More information about the Digitalmars-d-learn
mailing list