How to get the element type of an array?

Jon Degenhardt jond at noreply.com
Tue Aug 25 16:18:59 UTC 2020


On Tuesday, 25 August 2020 at 12:50:35 UTC, Steven Schveighoffer 
wrote:
>> The situation is still confusing though. If only 
>> 'std.range.ElementType' is imported, a static array does not 
>> have a 'front' member, but ElementType still gets the correct 
>> type. (This is where the documentation says it'll return void.)
>
> You are maybe thinking of how C works? D imports are different, 
> the code is defined the same no matter how it is imported. 
> *your* module cannot see std.range.primitives.front, but the 
> range module itself can see that UFCS function.

This is a good characteristic. But the reason it surprised me was 
that I expected to be able to manually expand the ElementType (or 
ElementEncodingType) template see the results of the expressions 
it uses.

    template ElementType(R)
    {
        static if (is(typeof(R.init.front.init) T))
            alias ElementType = T;
        else
            alias ElementType = void;
    }

So, yes, I was expecting this to behave like an inline code 
expansion.

Yesterday I was doing that for 'hasSlicing', which has a more 
complicated set of tests. I wanted to see exactly which 
expression in 'hasSlicing' was causing it to return false for a 
struct I wrote. (Turned out to be a test for 'length'.)

I'll have to be more careful about this.


More information about the Digitalmars-d-learn mailing list