auto & class members

Ali Çehreli acehreli at yahoo.com
Mon May 21 18:13:16 UTC 2018


On 05/20/2018 10:46 AM, Robert M. Münch wrote:

 > But I still don't understand why I can't write things explicitly but
 > have to use an alias for this.

Templatized range types work well when they are used as template 
arguments themselves.

When you need to keep a single type like 'b' (i.e. b is not a template), 
and when you need to set a variable like mySubStream to a dynamic 
object, the solution is to use inputObject():

import std.algorithm;
import std.range;

class a {
     int[] myStream = [ 1, 2, 42, 100 ];
}


int myMessage = 42;

class b {
     InputRange!int mySubStream;
}

void myFunc() {
     a myA = new a();
     b myB = new b();

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

     assert(myB.mySubStream.equal([myMessage]));
}

void main() {
     myFunc();
}

Now, mySubStream is a range variable that satisfies the input range 
interface and produces int elements. (Adjust accordingly.) You can use a 
more specialized range kind other than InputRange if the actual range 
supports it (e.g. ForwardRange!int, etc.):

 
http://ddili.org/ders/d.en/ranges_more.html#ix_ranges_more.inputRangeObject

   https://dlang.org/phobos/std_range_interfaces.html#inputRangeObject

Ali



More information about the Digitalmars-d-learn mailing list