Thread overview
Class allocation from tuple
May 21, 2009
bearophile
May 21, 2009
bearophile
May 21, 2009
Robert Fraser
May 21, 2009
I have a tuple of classes (D1 language), I'd like to instantiate one of them directly with new, but it seems I can't:

template Tuple(T...) { alias T Tuple; }

class Foo { static void foo(){} }
class Bar {}
alias Tuple!(Foo, Bar) ClassTuple;

void main() {
    alias ClassTuple[0] Foo0;
    new Foo0; // OK

    ClassTuple[0].foo(); // OK

    new ClassTuple[0]; // Not OK
    new (ClassTuple[0]); // Not OK
}

Can you tell me what the problem is?

Thank you and bye,
bearophile
May 21, 2009
On Thu, May 21, 2009 at 10:31 AM, bearophile <bearophileHUGS@lycos.com> wrote:
> I have a tuple of classes (D1 language), I'd like to instantiate one of them directly with new, but it seems I can't:
>
> template Tuple(T...) { alias T Tuple; }
>
> class Foo { static void foo(){} }
> class Bar {}
> alias Tuple!(Foo, Bar) ClassTuple;
>
> void main() {
>    alias ClassTuple[0] Foo0;
>    new Foo0; // OK
>
>    ClassTuple[0].foo(); // OK
>
>    new ClassTuple[0]; // Not OK
>    new (ClassTuple[0]); // Not OK
> }
>
> Can you tell me what the problem is?
>
> Thank you and bye,
> bearophile

I think it's just a shortcoming in the parser.  When it tries to parse the type following 'new', it interprets the brackets as meaning an array type, like when you do "new int[10]" or so.  That's why you get the error message "can't have array of (Foo, Bar)".
May 21, 2009
Jarrett Billingsley:

> When it tries to parse
> the type following 'new', it interprets the brackets as meaning an
> array type,<

I agree. But not even this works:
new (ClassTuple[0]);

Bye,
bearophile
May 21, 2009
On Thu, May 21, 2009 at 11:13 AM, bearophile <bearophileHUGS@lycos.com> wrote:
> Jarrett Billingsley:
>
>> When it tries to parse
>> the type following 'new', it interprets the brackets as meaning an
>> array type,<
>
> I agree. But not even this works:
> new (ClassTuple[0]);
>
> Bye,
> bearophile

Have you tried reading the errors?

dtest.d(187): basic type expected, not ;

It's parsing the parenthesized expression as an argument to new, then
wondering where your type is (like if you were doing "new(blah)
ClassName()").
May 21, 2009
bearophile wrote:
> I have a tuple of classes (D1 language), I'd like to instantiate one of them directly with new, but it seems I can't:
> 
> template Tuple(T...) { alias T Tuple; }
> 
> class Foo { static void foo(){} }
> class Bar {}
> alias Tuple!(Foo, Bar) ClassTuple;
> 
> void main() {
>     alias ClassTuple[0] Foo0;
>     new Foo0; // OK
> 
>     ClassTuple[0].foo(); // OK
> 
>     new ClassTuple[0]; // Not OK
>     new (ClassTuple[0]); // Not OK
> }
> 
> Can you tell me what the problem is?
> 
> Thank you and bye,
> bearophile

alias