Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
November 07, 2010 Re: array of elements of various subclasses | ||||
---|---|---|---|---|
| ||||
On Sun, 7 Nov 2010 16:17:38 +0100 spir <denis.spir@gmail.com> wrote: And I'd like to know, as a possible workaround, if there is a way to save a variadic arg list: class C { ??? xs; this(X xs...) { this.xs = xs; } } Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com |
November 07, 2010 Re: array of elements of various subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | spir Wrote:
> On Sun, 7 Nov 2010 16:17:38 +0100
> spir <denis.spir@gmail.com> wrote:
>
> And I'd like to know, as a possible workaround, if there is a way to save a variadic arg list:
> class C {
> ??? xs;
> this(X xs...) {
> this.xs = xs;
> }
> }
>
> Denis
> -- -- -- -- -- -- --
> vit esse estrany ☣
>
> spir.wikidot.com
>
You it is a typed variadic list (as shown above) it is just an array of that type so you should be able to save it to an X[]. If you are using the unsafe version then, no.
|
November 07, 2010 Re: array of elements of various subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | On 07.11.2010 18:49, spir wrote: > On Sun, 7 Nov 2010 16:17:38 +0100 > spir<denis.spir@gmail.com> wrote: > > And I'd like to know, as a possible workaround, if there is a way to save a variadic arg list: > class C { > ??? xs; > this(X xs...) { > this.xs = xs; > } > } > > Denis > -- -- -- -- -- -- -- > vit esse estrany ☣ > > spir.wikidot.com > Tuple!(X) from std.typecons should do -- Dmitry Olshansky |
November 07, 2010 Re: array of elements of various subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | On Sun, 07 Nov 2010 20:29:40 +0300
Dmitry Olshansky <dmitry.olsh@gmail.com> wrote:
> On 07.11.2010 18:49, spir wrote:
> > On Sun, 7 Nov 2010 16:17:38 +0100 spir<denis.spir@gmail.com> wrote:
> >
> > And I'd like to know, as a possible workaround, if there is a way to save a variadic arg list:
> > class C {
> > ??? xs;
> > this(X xs...) {
> > this.xs = xs;
> > }
> > }
> >
> > Denis
> > -- -- -- -- -- -- --
> > vit esse estrany ☣
> >
> > spir.wikidot.com
> >
> Tuple!(X) from std.typecons should do
>
Refused with the same error, plus two more:
DeeMatch.d(372): Error: cannot implicitly convert expression (patterns) of type DeeMatch.Pattern to Tuple!(Pattern)
DeeMatch.d(380): Error: no property 'opApply' for type 'Tuple!(Pattern)'
DeeMatch.d(380): Error: opApply() function for Tuple!(Pattern) must return an int
Actually, seems Tuple!(Pattern) defines a type for singleton Pattern tuples (Tuple!(A,B) is a type for 2-tuples). Not for a variadic list of Pattern element.
The two other errors refer to where I try to use the field 'patterns' in a foreach loop.
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
|
November 07, 2010 Re: array of elements of various subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On Sun, 07 Nov 2010 11:24:37 -0500
Jesse Phillips <jessekphillips+D@gmail.com> wrote:
> > And I'd like to know, as a possible workaround, if there is a way to save a variadic arg list:
> > class C {
> > ??? xs;
> > this(X xs...) {
> > this.xs = xs;
> > }
> > }
> >
> > Denis
> > -- -- -- -- -- -- --
> > vit esse estrany ☣
> >
> > spir.wikidot.com
> >
>
> You it is a typed variadic list (as shown above) it is just an array of that type so you should be able to save it to an X[]. If you are using the unsafe version then, no.
No, in fact a variadic param list is not an array -- this is according to TDPL an iternal type of the language. Trying to assign it to an X[] var throws:
DeeMatch.d(371): Error: cannot implicitly convert expression (patterns) of type DeeMatch.Pattern to Pattern[]
Well, actually, the type of the expression given in the error (DeeMatch.Pattern) is wrong: this is the type of each element of the variadic list.
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
|
November 07, 2010 Re: array of elements of various subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | spir Wrote: > No, in fact a variadic param list is not an array -- this is according to TDPL an iternal type of the language. Trying to assign it to an X[] var throws: > DeeMatch.d(371): Error: cannot implicitly convert expression (patterns) of type DeeMatch.Pattern to Pattern[] > Well, actually, the type of the expression given in the error (DeeMatch.Pattern) is wrong: this is the type of each element of the variadic list. Well I took the example for type safe variadics and added an int[] fish storing the array... So am I missing something? http://ideone.com/Kyi78 |
November 07, 2010 Re: array of elements of various subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On Sun, 07 Nov 2010 14:08:17 -0500
Jesse Phillips <jessekphillips+D@gmail.com> wrote:
> spir Wrote:
>
> > No, in fact a variadic param list is not an array -- this is according to TDPL an iternal type of the language. Trying to assign it to an X[] var throws:
> > DeeMatch.d(371): Error: cannot implicitly convert expression (patterns) of type DeeMatch.Pattern to Pattern[]
> > Well, actually, the type of the expression given in the error (DeeMatch.Pattern) is wrong: this is the type of each element of the variadic list.
>
> Well I took the example for type safe variadics and added an int[] fish storing the array... So am I missing something?
>
> http://ideone.com/Kyi78
No, you are right... I have certainly done an error when trying. Thank you.
Note: this works also with elements of various subtypes (so I can use this method to replace array parameters of type constructors):
class E {}
class E1 : E {
int i;
this(int i...) { this.i = i; }
override string toString () { return format("E1(%s)", this.i); }
}
class E2 : E {
float f;
this(float f...) { this.f = f; }
override string toString () { return format("E2(%s)", this.f); }
}
class C {
E[] ee;
this(E[] ee...) {
this.ee = ee;
}
}
//~ class C {
//~ int[] ii;
//~ this(int[] ii...) {
//~ this.ii = ii;
//~ }
//~ }
void main () {
//~ auto c = new C(1,2,3);
//~ writeln(c.ii);
auto c = new C(new E1(1), new E2(1.1));
writeln(c.ee);
}
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
|
November 08, 2010 Re: array of elements of various subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | On 07/11/2010 15:49, spir wrote:
<snip>
> And I'd like to know, as a possible workaround, if there is a way to save a variadic arg list:
> class C {
> ??? xs;
> this(X xs...) {
> this.xs = xs;
> }
> }
What exactly are you trying to do here?
If you declare it like that, you are declaring a function that takes a single object of class X, called by passing in the arguments of X's constructor. And in any case, that code won't work, because the object may be allocated on the stack.
ISTM what you're really trying to do is this:
class C {
X[] xs;
this(X[] xs...) {
this.xs = xs.dup;
}
}
But if all you want to do is return an array, you might as well use a template function:
T[] array(T)(T[] data...) {
return data.dup;
}
which you can instantiate either implicitly (for those cases where it works) or explicitly (for other cases).
Stewart.
|
Copyright © 1999-2021 by the D Language Foundation