digitalmars.D.learn - Class allocation from tuple
- bearophile (15/15) May 21 2009 I have a tuple of classes (D1 language), I'd like to instantiate one of ...
- Jarrett Billingsley (7/22) May 21 2009 hem directly with new, but it seems I can't:
- bearophile (5/8) May 21 2009 I agree. But not even this works:
- Jarrett Billingsley (6/14) May 21 2009 Have you tried reading the errors?
- Robert Fraser (2/24) May 21 2009 alias
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> 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, bearophileI 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: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()").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
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, bearophilealias
May 21 2009