On Monday, 1 August 2016 at 13:52:56 UTC, Jonathan M Davis wrote:
An array does not implement RandomAccessFinite, which is an
interface that you created. So, a function that takes a
RandomAccessFinite is not going to accept an array. A dynamic
array will match isRandomAccessRange, but that has nothing to
do with interfaces.
It's ok for me to say, that some types do not implement an
interface to show some abilities, even if the interface, which
has to be implemented to show the same abilities is
given/known/public/exposed... etc...
So, I think this part is ok now, I think...
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?
What's strange? If you want to accept any integer type, then
isIntegral!T would do it. A _better_ thing to do would be to
make it so that it's just size_t and not templatize the type,
since indices really should be size_t normally (and if the
system is 32-bit, then isIntegral!T will accept long and ulong,
whereas size_t is uint, and if you pass a long, you'll get a
compilation error for arrays, since they take size_t for
indexing; it won't matter for 64-bit though, since size_t is
ulong there). So,
auto f1(R)(size_t index, R range)
if(isRandomAccessRange!R)
{
return range[index];
}
would be better, but aside from the 32-bit issues, isIntegral
will work.
- Jonathan M Davis
This goes in a different direction I want to. I don't have
anything against simplification to size_t, indeed I have it in
this way currently. But what I want is something like the
following:
having
alias MyIndex = int
and
MyIndex s = MyInd(1);
writeln(f1(s, arr)); //gives 42, as expected
Now, I want to define a
struct S
{
int index;
??? what else ??? // alias index this; doesn't help
}
and still being able to have
alias MyIndex = S
and nothing else should be changed.