www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - iota result as member variable

reply Alex <sascha.orlov gmail.com> writes:
Hi everybody,
doing some optimization on my code, I faced some strange question:
how to save a iota result in a class member?

Say I have
class A
{
     ??? member;

     auto testIter4()
     {
         return iota(0,5);
     }
}

void main()
{
     A a = new A();
     a.member = testIter4();
}

how would I declare the member?

What I found till now is this:
http://comments.gmane.org/gmane.comp.lang.d.learn/60129
where it is said, that I could use
inputRangeObject(testIter4)
and declare my member as InputRange!int

But then the random access is gone and, furthermore, looking into 
the source of std/range/interfaces.d found some lines (about line 
nr. 110) about performance and that the InputRangeObject has a 
performance penalty of about 3 times over using the iota struct 
directly. So, I could declare my member as typeof(iota(0))

Did I miss something?
Mar 23 2016
next sibling parent Alex <sascha.orlov gmail.com> writes:
As a comment on my own post:
I’m aware, that there are some different return types from 
functions like iota. And I’m also aware, that there are much less 
different range types. I can, maybe, define what kind of range 
type I want to have, the question is, how to map all the 
different function results to this one interface I need with as 
less performance penalty as possible
Mar 24 2016
prev sibling parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Thursday, 24 March 2016 at 06:54:25 UTC, Alex wrote:
 Hi everybody,
 doing some optimization on my code, I faced some strange 
 question:
 how to save a iota result in a class member?

 Say I have
 class A
 {
     ??? member;

     auto testIter4()
     {
         return iota(0,5);
     }
 }

 void main()
 {
     A a = new A();
     a.member = testIter4();
 }

 how would I declare the member?
Yeah this is one of the downsides of voldermort types. In these cases typeof and ReturnType are your friend. It often takes me a couple of tries to get it right, but the following seems to work: import std.traits : ReturnType; import std.range : iota; class A { ReturnType!(A.testIter4) member; auto testIter4() { return iota(0,5); } } void main() { A a = new A(); a.member = a.testIter4(); }
Mar 24 2016
parent Alex <sascha.orlov gmail.com> writes:
On Thursday, 24 March 2016 at 08:13:27 UTC, Edwin van Leeuwen 
wrote:
 Yeah this is one of the downsides of voldermort types. In these 
 cases typeof and ReturnType are your friend. It often takes me 
 a couple of tries to get it right, but the following seems to 
 work:

 import std.traits : ReturnType;
 import std.range : iota;
 class A
 {
     ReturnType!(A.testIter4) member;
     auto testIter4()
     {
         return iota(0,5);
     }
 }

 void main()
 {

      A a = new A();
      a.member = a.testIter4();
 	
 }
Ah... thanks! The "ReturnType" is what I looked for. This also makes some kind of semantic statement about "how slim should be the interface of my class members"...
Mar 24 2016