digitalmars.D.learn - iota result as member variable
- Alex (29/29) Mar 23 2016 Hi everybody,
- Alex (7/7) Mar 24 2016 As a comment on my own post:
- Edwin van Leeuwen (20/39) Mar 24 2016 Yeah this is one of the downsides of voldermort types. In these
- Alex (5/25) Mar 24 2016 Ah... thanks! The "ReturnType" is what I looked for. This also
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
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
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
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









Alex <sascha.orlov gmail.com> 