www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Class allocation from tuple

reply bearophile <bearophileHUGS lycos.com> writes:
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
next sibling parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Thu, May 21, 2009 at 10:31 AM, bearophile <bearophileHUGS lycos.com> wro=
te:
 I have a tuple of classes (D1 language), I'd like to instantiate one of t=
hem 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() {
 =A0 =A0alias ClassTuple[0] Foo0;
 =A0 =A0new Foo0; // OK

 =A0 =A0ClassTuple[0].foo(); // OK

 =A0 =A0new ClassTuple[0]; // Not OK
 =A0 =A0new (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
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
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
prev sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
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
May 21 2009