Thread overview
iota result as member variable
Mar 24, 2016
Alex
Mar 24, 2016
Alex
Mar 24, 2016
Edwin van Leeuwen
Mar 24, 2016
Alex
March 24, 2016
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?
March 24, 2016
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
March 24, 2016
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();
	
}
March 24, 2016
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"...