Thread overview
How do I check if a variable is a multidimensional (2D) array?
Jul 12, 2021
Kirill
Jul 12, 2021
jfondren
Jul 12, 2021
Kirill
July 12, 2021

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.

July 12, 2021

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

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.

July 12, 2021

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

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!