August 18, 2021

On Tuesday, 17 August 2021 at 18:27:21 UTC, Steven Schveighoffer wrote:

>

According to my tests, it prefers the T version over the static array version. Which leads me to believe that it prefers a dynamic array over a static one. In fact, if I comment out the T version, it doesn't compile. It literally will not pick that specialization, even if it can interpret the literal that way.

which is really bizarre, since if you do it without specializations, but just spelling out all the template components (as in your alternative workaround), it WILL pick that one over a dynamic array one.

Oh my, that's weird... Not meant to bash but given all I've seen of argument deduction & templates & specializations... I think the implementation needs some serious rework 😅


Interestingly enough my approach will not even work for 2d array literals. It will manage going to int[][2], but int[2][2] is one step too far. Which is a real bummer. :(
That is, it won't figure it out itself, but when you call foo(T, uintL)(T[L][L]... using an explicit foo!(int, 2) it will work. Even though it manages T[L] just fine.
Meanwhile T[2][L] and T[L][2] won't work when called with a 2x2 array literal.

August 18, 2021

On Tuesday, 17 August 2021 at 18:46:05 UTC, Ali Çehreli wrote:

>

I don't have such problems because I am not smart enough to understand that syntax so I don't use it. :) I use template constraints (which have other problems).

Yeah, they seem to be a bit more trustworthy to some extent.

>

If you want 2 dimensional arrays, then you can use

import std.range;

isArray!T && (isArray!(ElementType!T))

I tried looking into how isArray is defined. Like, does being able to index mean it's an array, or are these only static &/or dynamic arrays? Though I couldn't understand the sourcecode.
Hence I just use(d) is(typeof(variable[0])) and is(typeof(variable[0][0])).

August 18, 2021

On Wednesday, 18 August 2021 at 11:10:49 UTC, Rekel wrote:

>

I tried looking into how isArray is defined. Like, does being able to index mean it's an array, or are these only static &/or dynamic arrays?

Did you read the documentation?

https://phobos.dpldocs.info/std.traits.isArray.html

August 18, 2021

On Wednesday, 18 August 2021 at 13:35:07 UTC, Paul Backus wrote:

>

On Wednesday, 18 August 2021 at 11:10:49 UTC, Rekel wrote:

>

I tried looking into how isArray is defined. Like, does being able to index mean it's an array, or are these only static &/or dynamic arrays?

Did you read the documentation?

https://phobos.dpldocs.info/std.traits.isArray.html

Ah it's specifically static & dynamic arrays, I see.

August 18, 2021
On 8/18/21 4:10 AM, Rekel wrote:

>>   isArray!T && (isArray!(ElementType!T))
>
> I tried looking into how isArray is defined. Like, does being able to
> index mean it's an array, or are these only static &/or dynamic arrays?

The definitions are in phobos/std/traits.d

  enum bool isArray(T) = isStaticArray!T || isDynamicArray!T;

isStaticArray uses the compiler's __traits feature:

  enum bool isStaticArray(T) = __traits(isStaticArray, T);

isDynamicArray uses the is expression but apparently has some history:

template isDynamicArray(T)
{
    static if (is(T == U[], U))
        enum bool isDynamicArray = true;
    else static if (is(T U == enum))
        // BUG: isDynamicArray / isStaticArray considers enums
        // with appropriate base types as dynamic/static arrays
        // Retain old behaviour for now, see
        // https://github.com/dlang/phobos/pull/7574
        enum bool isDynamicArray = isDynamicArray!U;
    else
        enum bool isDynamicArray = false;
}

Ali

1 2
Next ›   Last »