How to get element type of a slice?

jmh530 john.michael.hall at gmail.com
Wed Aug 18 20:05:34 UTC 2021


On Tuesday, 17 August 2021 at 14:40:20 UTC, Ferhat Kurtulmuş 
wrote:
> [snip]
>
> Very informative, thanks. My code is lying here[1]. I want my 
> struct to accept 2d static arrays, random access ranges, and 
> "std.container.Array". I could achieve it with its present 
> form, and I will probably slightly modify it based on your 
> comments.
>
> [1]: 
> https://github.com/aferust/earcut-d/blob/master/source/earcutd.d#L34

If it would only accept dynamic arrays, you could use something 
like below

```d
import std.traits: isDynamicArray;

template DynamicArrayOf(T : U[], U)
     if (isDynamicArray!T)
{
     alias DynamicArrayOf = U;
}

struct Point {}

void main()
{
     static assert(is(DynamicArrayOf!(int[]) == int));
     static assert(is(DynamicArrayOf!(Point[]) == Point));
}
```


More information about the Digitalmars-d-learn mailing list