Indexing with an arbitrary type
Alex via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 1 04:06:54 PDT 2016
Hi everybody,
I have a question and a half on templates and ranges, this time.
Say, I have two functions:
auto f1(T, S)(T t, S s) if(isIntegral!T && isRandomAccessRange!S)
{
return s[t];
}
auto f2(T, S)(T t, RandomAccessFinite!S raf) if(isIntegral!T)
{
return raf[t];
}
and a
class myClass : RandomAccessFinite!ubyte {...}
which implements all the methods needed by the RandomAccessFinite
interface.
then, I could use this in my main by just
void main()
{
myClass mC = new myClass();
writeln(f2(1, mC));
writeln(f1(1, mC));
ubyte[] arr = [0, 42, 2, 3, 4];
writeln(f1(1, arr));
//writeln(f2(1, arr)); //this fails and is the first part of
the question.
}
so, the first question is, why the method using the
RandomAccessFinite interface fails on using an array? Did I miss
a method, which is not supported by an array?
But the main question is about the other part, about the
constraint to the first parameter to my functions.
It seems strange to me to use "isIntegral" here, as this is some
kind of unrelated for something used as an index.
Is there anything more appropriate to check? What kind of
interface/trait has an index to fulfill?
More information about the Digitalmars-d-learn
mailing list