Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #994 (closed defect: fixed)

Opened 16 years ago

Last modified 16 years ago

tango.core.Traits: isStaticArray!(char[5][2]) fails

Reported by: od Assigned to: sean
Priority: major Milestone: 0.99.6
Component: Core Functionality Version: 0.99.5 Jascha
Keywords: Cc:

Description

static assert (isStaticArrayType!(char[5][2])); fails

import tango.core.Traits;

static assert (phobos_isStaticArray!(char[5][2])); // ok
static assert (isStaticArrayType!(char[5][2]));    // fails

void main() {}

/*
 * How phobos handles this:
 */
template phobos_isStaticArray_impl(T)
{
    const T inst = void;

    static if (is(typeof(T.length)))
    {
	static if (!is(typeof(T) == typeof(T.init)))
	{			// abuses the fact that int[5].init == int
	    static if (is(T == typeof(T[0])[inst.length]))
	    {	// sanity check. this check alone isn't enough because dmd complains about dynamic arrays
		const bool res = true;
	    }
	    else
		const bool res = false;
	}
	else
	    const bool res = false;
    }
    else
    {
	    const bool res = false;
    }
}
/**
 * Detect whether type T is a static array.
 */
template phobos_isStaticArray(T)
{
    const bool phobos_isStaticArray = phobos_isStaticArray_impl!(T).res;
}

Outputs: Line 4: static assert is false

The reason is that not just is((char[2]).init==char) holds but also is((char[5][2]).init==char). So in tango.core.Traits.isStaticArrayType you end up with a static if (is(char[10]==char[5][2])). I suggest to use the phobos version which is proof against this.

Change History

04/17/08 05:43:26 changed by sean

  • status changed from new to closed.
  • resolution set to fixed.

(In [3440]) Changed definition of isStaticArrayType. This closes #994

07/09/08 17:56:16 changed by larsivi

(In [3735]) More unittests, refs #994