Thread overview
Need size of rightmost array, not type...
Feb 22, 2007
Ary Manzana
Feb 22, 2007
Max Samukha
Feb 22, 2007
Ary Manzana
February 22, 2007
This compiles fine:

----------------------------------------
template Foo() {
	const int Foo = 3;
}

void one() {
	int x = Foo!();
	char[] buf = new char[x];
}
----------------------------------------

However this:

----------------------------------------
template Foo() {
	const int Foo = 3;
}

void two() {
	char[] buf = new char[Foo!()];
}
----------------------------------------

main.d(6): need size of rightmost array, not type Foo!()

Why?
February 22, 2007
On Wed, 21 Feb 2007 23:59:21 -0300, Ary Manzana <ary@esperanto.org.ar> wrote:

>This compiles fine:
>
>----------------------------------------
>template Foo() {
>	const int Foo = 3;
>}
>
>void one() {
>	int x = Foo!();
>	char[] buf = new char[x];
>}
>----------------------------------------
>
>However this:
>
>----------------------------------------
>template Foo() {
>	const int Foo = 3;
>}
>
>void two() {
>	char[] buf = new char[Foo!()];
>}
>----------------------------------------
>
>main.d(6): need size of rightmost array, not type Foo!()
>
>Why?

Seems to be a bug. If your array is not big, you might consider using a static array as the size is known at compile time.

char[Foo!()] buf; works

February 22, 2007
Max Samukha escribió:
> On Wed, 21 Feb 2007 23:59:21 -0300, Ary Manzana <ary@esperanto.org.ar>
> wrote:
> 
>> This compiles fine:
>>
>> ----------------------------------------
>> template Foo() {
>> 	const int Foo = 3;
>> }
>>
>> void one() {
>> 	int x = Foo!();
>> 	char[] buf = new char[x];
>> }
>> ----------------------------------------
>>
>> However this:
>>
>> ----------------------------------------
>> template Foo() {
>> 	const int Foo = 3;
>> }
>>
>> void two() {
>> 	char[] buf = new char[Foo!()];
>> }
>> ----------------------------------------
>>
>> main.d(6): need size of rightmost array, not type Foo!()
>>
>> Why?
> 
> Seems to be a bug. If your array is not big, you might consider using
> a static array as the size is known at compile time.
> 
> char[Foo!()] buf; works

Thanks Max.

Walter:

I don't know why this error appear. I was expecting it not to appear. I think the problem is the following functions:

parser.c
#############################################################
Expression *Parser::parseNewExp(Expression *thisexp)
{
    // ...

    #if LTORARRAYDECL
    t = parseBasicType();
    t = parseBasicType2(t);
    if (t->ty == Taarray)
    {
	Type *index = ((TypeAArray *)t)->index;

	Expression *e = index->toExpression();
	if (e)
	{   arguments = new Expressions();
	    arguments->push(e);
	    t = new TypeDArray(t->next);
	}
	else
	{
	    error("need size of rightmost array, not type %s", index->toChars());
	    return new NullExp(loc);
	}
    }
}
#######################################################

It seems e is null. However for the source code I gave, I think index is an instance of TypeIdentifier, so it is converted to a DotIdExp:

mtype.c
#######################################################
Expression *TypeIdentifier::toExpression()
{
    Expression *e = new IdentifierExp(loc, ident);
    for (int i = 0; i < idents.dim; i++)
    {
	Identifier *id = (Identifier *)idents.data[i];
	e = new DotIdExp(loc, e, id);
    }

    return e;
}
#######################################################

So:

1. I don't understand why e is null. Maybe index is not an instance of TypeIdentifier, but I think it is.
2. I think that function TypeIdentifier::toExpression() is incorrect. It is turning a type like "Foo!(a, b)" into an expression "Foo.a.b"... or am I misunderstanding something?