www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I check if a variable is a multidimensional (2D) array?

reply Kirill <kirill.saidov mail.com> writes:
I know there is isArray!T and similar functionality in 
std.traits. But I couldn't find the functionality that can help 
me check if I have a multidimensional array. Is there any? How do 
I create my own?

Thanks in advance.
Jul 11 2021
parent reply jfondren <julian.fondren gmail.com> writes:
On Monday, 12 July 2021 at 04:25:00 UTC, Kirill wrote:
 I know there is isArray!T and similar functionality in 
 std.traits. But I couldn't find the functionality that can help 
 me check if I have a multidimensional array. Is there any? How 
 do I create my own?

 Thanks in advance.
from https://github.com/PhilippeSigaud/D-templates-tutorial ```d template rank(T) { static if (is(T t == U[], U)) enum size_t rank = 1 + rank!(U); else enum size_t rank = 0; } unittest { int a; int[] b; int[][] c; assert(rank!(typeof(a)) == 0); assert(rank!(typeof(b)) == 1); assert(rank!(typeof(c)) == 2); } ``` as an example of a recursive template. There's also an implementation for ranges.
Jul 11 2021
parent Kirill <kirill.saidov mail.com> writes:
On Monday, 12 July 2021 at 05:08:29 UTC, jfondren wrote:
 On Monday, 12 July 2021 at 04:25:00 UTC, Kirill wrote:
 I know there is isArray!T and similar functionality in 
 std.traits. But I couldn't find the functionality that can 
 help me check if I have a multidimensional array. Is there 
 any? How do I create my own?

 Thanks in advance.
from https://github.com/PhilippeSigaud/D-templates-tutorial ```d template rank(T) { static if (is(T t == U[], U)) enum size_t rank = 1 + rank!(U); else enum size_t rank = 0; } unittest { int a; int[] b; int[][] c; assert(rank!(typeof(a)) == 0); assert(rank!(typeof(b)) == 1); assert(rank!(typeof(c)) == 2); } ``` as an example of a recursive template. There's also an implementation for ranges.
Thanks!
Jul 11 2021